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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应