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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失