doutuo8800 2013-05-07 13:35
浏览 58
已采纳

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

I have this script to open a web page and count every line with an img tag. However, it is not working. Could help me to find out the problem with the script? This array should hold info of every line but it is giving about only 1 line of its choice.

<?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条回答 默认 最新

  • doubangzhang6992 2013-05-07 13:51
    关注

    Your problem is this: $line = array($x , "true");
    You are assigning a new value to $line, not pushing the value into the array.

    Instead you should do either of this:

    $line[$x] = "true" // or false, whatever
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题