dp518158 2016-05-10 11:08
浏览 75
已采纳

正则表达式与div类

I am using regex to extract content between different classes of tags, however I got the output: No matches thus help is required. I do understand that xpath or DOM Document would be a better choice instead of using regex however my project require me to use regex as xpath or DOM document would affect the later part of the project. Thanks

// Read php file using curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/page1.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://localhost/page1.php/$2$3', $result);

//print out page1.php file content
#echo $result;
$data = $result;
$pattern = '{<!-- populate table from mysql database -->(.*?)\</tbody>}';
#$pattern = '{<div\s+class="name"\s*>((?:(?:(?!<div[^>]*>|</div>).)++(?=<div[^>]*>(?1)</div>)*))}si';
$matchcount = preg_match_all($pattern, $data, $matches);

if ($matchcount > 0) {
    for($i = 0; $i < $matchcount; $i++) {
        echo("
");
        echo($matches[1][$i]); 
    }
} else {
    echo('No matches');
}  

HTML:

<tbody>
<!-- populate table from mysql database -->
<div class="student_information">
    <tr>
        <div class="admin"><td>140009K</td></div>
        <div class="name"><td>Lee Tan</td></div>
        <div class="hp"><td>96655568</td></div>
        <div class="email"><td>140000K@gmail.com</td></div>
    </tr>
</div>
<div class="student_information">
    <tr>
        <div class="admin"><td>1411111A</td></div>
        <div class="name"><td>Sally Tan</td></div>
        <div class="hp"><td>.</td></div>
        <div class="email"><td>sally8@hotmail.com</td></div>
    </tr>
</div>
</tbody>

Output should be:

140009K
Lee Tan
96655568
140000K@gmail.com

1411111A
Sally Tan
83954441
sally8@hotmail.com

What's wrong? Need help!

  • 写回答

1条回答 默认 最新

  • doujian1954 2016-05-10 13:02
    关注
    preg_match_all("/<div class.*><td>(.*)<\/td><\/div>/", $input, $output);
    

    http://www.phpliveregex.com/p/fDE

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析