正则表达式与div类

时间:2016-05-10 11:08:22

标签: php regex

我正在使用正则表达式来提取不同类别的标签之间的内容,但是我得到了输出:没有匹配因此需要帮助。我确实理解xpath或DOM Document是一个更好的选择,而不是使用正则表达式,但是我的项目要求我使用正则表达式作为xpath或DOM文档会影响项目的后期部分。感谢

// 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("\n");
        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>

输出应为:

140009K
Lee Tan
96655568
140000K@gmail.com

1411111A
Sally Tan
83954441
sally8@hotmail.com

怎么了?需要帮忙!

1 个答案:

答案 0 :(得分:0)

preg_match_all("/<div class.*><td>(.*)<\/td><\/div>/", $input, $output);

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