正则表达式,获取值结束标记

时间:2015-11-21 04:40:12

标签: php preg-match

您好,这是我的代码:

<?php
require('/simple_html_dom.php');
$html = new simple_html_dom();
$html = file_get_html('proxys.html');

$items = array();
$re = "/<td class=\\\"t_ip\\\">\\s*((?:[0-9]{1,3}\\.){3}[0-9]{1,3})\\s*<\\/td>(?:.*?)*<td class=\"t_port\">(?:.*?)\\w+\\^\\w+\\^([0-9]{1,5})(?:.*?)<td class=\"t_type\">\\s*([0-9])(?:.*?)/"; 

        preg_match_all($re, $html, $matches, PREG_SET_ORDER);
        foreach ($matches as $val) {
        echo nl2br($val[1] . ':' . $val[2] . ' ' . $val[3] . "\n");
        };

?>

proxys.html

<td class="t_ip">104.131.248.140</td><td class="t_port">           <script type="text/javascript">           //<![CDATA[             document.write(BigBlind^BigBlind^60088);           //]]>           </script>50088         </td><td class="t_type">     5         </td><td class="t_ip">79.101.32.14</td><td class="t_port">           <script type="text/javascript">           //<![CDATA[             document.write(Polymorth^Polymorth^1080);           //]]>           </script>45080         </td>

问题是该值获得了**** document.write的“60088”(BigBlind ^ BigBlind ^ 60088 ); ****

104.131.248.140:    60088 5
79.101.32.14:       1080 4

并希望获得&lt;的值/ script&gt; 50088

104.131.248.140:    50088 5
79.101.32.14:       45080 4

我迷失了正常表达,谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

您可以尝试使用DOMDocument,如

$html = '<td class="t_ip">104.131.248.140</td><td class="t_port">           <script type="text/javascript">           //<![CDATA[             document.write(BigBlind^BigBlind^60088);           //]]>           </script>50088         </td><td class="t_type">     5         </td><td class="t_ip">79.101.32.14</td><td class="t_port">           <script type="text/javascript">           //<![CDATA[             document.write(Polymorth^Polymorth^1080);           //]]>           </script>45080         </td>';

$dom = new DOMDocument;
$dom->loadHTML($html);
$root = $dom->documentElement;
$tds = $root->getElementsByTagName("td");
foreach($tds as $key => $value){
    echo $value->parentNode->textContent."<br>";
}