dongwu5743 2014-10-15 13:50
浏览 203
已采纳

php文件获取内容,搜索和显示

I have some content it another website that i would like to show my users, i use file get content function.

but i dont need to display the whole content, just two rows, The current product and the next product. the content is arranged with html tables.

my code:

   <?php
$url = "http://the second website.com";
$str = file_get_contents($url);

$lines = explode("<tr>", $str);

foreach ($lines as $newline) {
    echo '<tr>' . $newline . '</tr>'; -- This prints all lines.
}
?>

How to echo only the specific lines ? My needed line are start with the strings "current product" and "next product". Do i need to use an array ? or do in need to use a search string function ?

Thanks all.

  • 写回答

2条回答 默认 最新

  • dongmao3131 2014-10-15 13:58
    关注

    You could do this with preg_match_all like this:

    $table = <<<EOS
    <table>
       <tr>
          <td>1.1</td>
          <td>1.2</td>
       </tr>
       <tr>
          <td>2.1</td>
          <td>2.2</td>
       </tr>
    </table>
    EOS;
    
    preg_match_all('/<tr.*?>(.*?)<\/tr>/si', $table, $matches);
    
    print_r($matches[1]);
    

    The output is:

    Array
    (
        [0] => 
          <td>1.1</td>
          <td>1.2</td>
    
        [1] => 
          <td>2.1</td>
          <td>2.2</td>
    
    )
    

    You could also read about PHP DOM Inspector.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助