views:

68

answers:

1

Hey guys,

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 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.

+2  A: 

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.

JGB146
(Edited: didn't see the "_all" for preg_match_all)... You also shouldn't need the s modifier (single line mode) because there aren't any periods in the regex.
Tim
@Tim: The s modifier was just in case the input contained a block of html that spanned multiple lines. For the input specified by OP or myself, this is not needed, but there could be other cases in which it would be.
JGB146
As predicted, it works perfectly. Thanks a million for your answer and sorry if I didn't respond earlier, I posted this about an hour before getting to bed. Thank's again.