如何在PHP中搜索html文件中的子字符串?

时间:2013-05-07 13:35:13

标签: php substring

我有这个脚本来打开一个网页,并使用img标记计算每一行。但是,它不起作用。可以帮我找出脚本的问题吗?这个数组应该包含每一行的信息,但它只提供了一行选择。

<?php
$a = 'www.exaple.com/examplepage.html'; //page i want to search
$b = fopens($a , "r"); //to open the page for viewing source
$line = array("0" => "false"); //to keep record of lines with img tag we dont have  line 0 so dont worry
$x = 0; //varialble to hold no. of lines
while(!feof($b)) {   //search every line of file upto the end
    $x = $x+1; //update line every it loops
    $pos = strrpos(fgets($b) ,"<img"); //seach for the img tag
    if($pos === false) { $line = array($x , "false"); } //keep record of line without img tag as fasle
    else { $line = array($x , "true"); } //keep record of line with img tag as true
}
print_r($line);
fclose($b);
?>

1 个答案:

答案 0 :(得分:1)

你的问题是:$line = array($x , "true");
您正在为$line分配一个新值,而不是将值推送到数组中。

相反,您应该执行以下任一操作:

$line[$x] = "true" // or false, whatever