English 中文(简体)
PHP 元素ById、属性和字符串 PHP 问题
原标题:PHP Issues with GetElementById, getAttribute, and StrPos
  • 时间:2012-05-21 17:16:43
  •  标签:
  • php

我是PHP的相对初学者,而我正试图开发一个网络剪切脚本。 脚本的设计是为了抓取一个 VBulletin 论坛页面, 然后通过页面上的超链接分析找到包含特定“ id” 元素的超链接( 即只有指向在论坛上张贴的信息线索的链接) 。 每个想要的链接都包含一个“ id” 元素, 开始“ thread_ title_ [Thread# here] 。 我提出一个想法, 即用STRPOS 来过滤所收集的链接中的每个“ id” 元素, 检查它们是否包含“ thread_ title” 碎片。 不幸的是, 我的努力似乎没有产生结果 。

我会在下面贴上代码节录... 冒着被贴上完全诺比的标签的风险。

   $d = new domdocument();

   $d->loadHTMLfile("forum3.html");

    $links = $d->getElementsByTagName( a );
    echo  <html xmlns="http://www.w3.org/1999/xhtml" encoding="utf-8" lang="ar-sa"> ;

    foreach ($links as $link)
    {
        $threadTitleExists = $link->getAttribute( id );
        $pos = strpos($threadTitleExists,  thread_title );  
        $threadTitle = $link->nodeValue;
        if ($link->hasAttribute( id ) && ($pos==0))
        {    
             $threadTitle = trim(preg_replace( #/s*([^)]*)/ ,    , $threadTitle));
             echo "Thread number: " . $threadTitleExists . "<br>Thread title: " . $threadTitle . "<p>";
        }

         else 
         {

             continue;

         }

    }
最佳回答

更改线条

if ($link->hasAttribute( id ) && $pos != false)

if ($link->hasAttribute( id ) && $pos !== false)

strpos() returns 0 if the haystack begins with the needle, which evaluates as false when using the loosely typed comparison opera至r. There s a warning on the manual page (linked) 至 use the === opera至r instead.

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签