0

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?

2
  • 1
    Could you give an example of what you want to match and what you don't want to match. Cuz i can't figure it out with your description Commented Mar 26, 2015 at 16:49
  • Escape the square brackets: /\b\[download id="2"\]\b/i. Commented Mar 26, 2015 at 16:50

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.