简体   繁体   English

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

[英]Preg match text in php between html tags

Hello I would like to use preg_match in PHP to parse the "Desired text" out of the following from a html document 您好我想在PHP中使用preg_match来解析html文档中的“Desired text”

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

Ordinarily I would use simple_html_dom for such things but on this occasion it cannot be used (the above element doesn't appear in every desired div tag so I'm forced to use this approach to keep track of exactly when it doesn't appear and then adjust my array from simple_html_dom accordingly). 通常我会使用simple_html_dom这样的东西,但在这种情况下它不能使用(上面的元素没有出现在每个所需的div标签中,所以我被迫使用这种方法来准确跟踪它何时没有出现和然后相应地从simple_html_dom调整我的数组)。

Anyway, this would solve my problem. 无论如何,这将解决我的问题。

Thanks so much. 非常感谢。

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

if you want to return multiple matches then need to use preg_match_all(). 如果你想返回多个匹配,那么需要使用preg_match_all()。 You then loop through the second result group ($match[1]) to get just the content between tags. 然后循环遍历第二个结果组($ 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 

What if the string you're matching has multiple lines and is: 如果您匹配的字符串有多行并且是:

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

That pattern would match once, and the match would be everything in the string. 该模式将匹配一次,匹配将是字符串中的所有内容。

I think a better pattern is: 我认为更好的模式是:

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

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

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