Gustav
Gustav

Reputation: 109

PHP Preg_match always return true

im trying to retreive a list of Wordpress Posts with get_posts,

After that, i want to search in the post_content if there any match of a specific string.

right now, my code is:

if(preg_match('/\b[download id="2"]\b/i', $value->post_content)){
       echo('match');
}else{
       echo('nomatch');
}

But it return alway a match.

What i'm doing wrong?

Upvotes: 0

Views: 320

Answers (1)

Styphon
Styphon

Reputation: 10447

Square brackets means to match against any of the characters specified, so in your case it's matching d or o or w or n or l or a or or i or = or " or 2.

If you want to match download id="2" in a string then you need to use /(download id="2")/i. If you want to match [download id="2"] then you need to escape the square brackets like this: /(\[download id="2"\])/i

Upvotes: 1

Related Questions