繁体   English   中英

preg匹配html标签之间的PHP文本

[英]Preg match text in php between html tags

您好我想在PHP中使用preg_match来解析html文档中的“Desired text”

<p class="review"> Desired text </p>

通常我会使用simple_html_dom这样的东西,但在这种情况下它不能使用(上面的元素没有出现在每个所需的div标签中,所以我被迫使用这种方法来准确跟踪它何时没有出现和然后相应地从simple_html_dom调整我的数组)。

无论如何,这将解决我的问题。

非常感谢。

preg_match("'<p class=\"review\">(.*?)</p>'si", $source, $match);
if($match) echo "result=".$match[1];

如果你想返回多个匹配,那么需要使用preg_match_all()。 然后循环遍历第二个结果组($ match [1])以获取标记之间的内容。

$source = "<p class=\"review\"> Desired text1 </p>".
"<p class=\"review\"> Desired text2 </p>".
"<p class=\"review\"> Desired text3 </p>";


    preg_match_all("'<p class=\"review\">(.*?)</p>'si", $source, $match);

    foreach($match[1] as $val)
    {
        echo $val."<br>";


    }

Outputs:

Desired text1
Desired text2
Desired text3 

如果您匹配的字符串有多行并且是:

<p class="review"> Desired text1 </p>
<p class="review"> Desired text2 </p>
<p class="review"> Desired text3 </p>

该模式将匹配一次,匹配将是字符串中的所有内容。

我认为更好的模式是:

"'<p class=\"review\">([^<]*)</p>'si"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM