douhan9467 2011-11-23 19:39
浏览 12
已采纳

PHP Parse文件,替换不是必需的项目出错

I'm trying to parse each IP line from the following file (loading from the web) and I'm going to store the values in database so i'm looking to put them in to an array.

The file its loading has the following source:

12174 in store for taking<hr>221.223.89.99:8909

<br>123.116.123.71:8909

<br>221.10.162.40:8909

<br>222.135.5.38:8909

<br>120.87.121.122:8909

<br>118.77.254.242:8909

<br>218.6.19.14:8909

<br>113.64.124.85:8909

<br>123.118.243.239:8909

<br>124.205.154.181:8909

<br>124.117.13.116:8909

<br>183.7.223.212:8909

<br>112.239.205.245:8909

<br>118.116.235.156:8909

<br>27.16.28.174:8909

<br>222.221.142.59:8909

<br>114.86.40.251:8909

<br>111.225.105.142:8909

<br>115.56.86.62:8909

<br>59.51.108.142:8909

<br>222.219.39.194:8909

<br>114.244.252.246:8909

<br>202.194.148.41:8909

<br>113.94.174.239:8909

<br><hr>total£º 24¡£

So I guess I'm looking to take everything between the <hr>'s and add each line line by line.

However doing the following doesn't seem to be working (in terms of stripping it the parts i dont' want)

<?php
$fileurl = "**MASKED**";

$lines = file($fileurl);

foreach ($lines as $line_num => $line) {

    $line2 = strstr($line, 'taking', 'true');
    $line3 = str_replace($line2, '', $line);
    print_r($line3);

}

?>
  • 写回答

2条回答 默认 最新

  • dtnat80842 2011-11-23 19:49
    关注

    If you want to add the values to an array, why not doing that directly inside the loop? I'd do something like this:

    $output = array();
    foreach ($lines as $line) {
        if(preg_match("/<br>\d/", $line)) {
            $output[] = substr($line, 4);
        }
    }
    print_r($output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题