开发者

Regex - Grab a specific word within specific tags

I don't consider myself a PHP "noob", but regular expressions are still new to me.

I'm doing a CURL where I receive a list of comments. Every comment has this HTML structure:

<div class="comment-text">the comment</div>

What I want is simple: I want to get, from a preg_match_all, the comments that have the word "cool" in this specific DIV开发者_如何学C tag.

What I have so far:

preg_match_all("#<div class=\"comment-text\">\bcool\b</div>#Uis", $getcommentlist, $matchescomment);

Sadly, this doesn't work. But if the REGEX is simply #\bcool\b#Uis, it will work. But I really want to capture the word "cool" in those tags.

I know I could do 2 regular expressions (one that gets all the comments, the other that filters each of them to capture the word "cool"), but I was wondering how could I do this in one preg_match_all?

I don't think I'm far from the solution, but somehow I just can't find it. Something's definitely missing.

Thank you for your time.


This should give you what you're looking for, and provide some flexibility if you want to change things a bit:

$input = '<div class="comment-text">the comment</div><div class="comment-text">cool</div><div class="comment-text">this one is cool too</div><div class="comment-text">ool</div>';
$class="comment-text";
$text="cool";
$pattern = '#<div class="'.$class.'">([^<]*'.$text.'[^<]*)</div>#s';
preg_match_all($pattern, $input, $matches);

Obviously, you need to set your input as the value for $input. After this runs, an array of the <div>s that matched will be in $matches[0] and an array of the text that matched will be in $matches[1]

You can change the class of div to match or the within-div text to require by changing the $class and $text values, respectively.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜