0

Hi I have a little plugin system, I use this shortcode for example: {google}stackoverflow{/google}

I want it to replace that part with a link to the google search link, in this case: stackoverflow

How should I do this? I have this but it doesn't work:

preg_replace('#{google}([^{]+){/google}#i', '<a href="http://www.google.com?s=$1">$1</a>', $content);
3
  • What about it doesn't work? The regex doesn't return matches? There's an error message? If so, what's the error message? Commented Aug 22, 2012 at 19:46
  • { and } are reserved characters, BTW. Commented Aug 22, 2012 at 19:46
  • 1
    Be aware though that this piece of code is highly vulnerable to XSS in case it may be used by not only "trustworthy" users. Commented Aug 22, 2012 at 19:48

4 Answers 4

1

You need to escape the curly braces, i.e. use \{ and \}.

preg_replace('#\{google\}([^{]+)\{/google\}#i', '<a href="http://www.google.com?s=$1">$1</a>', $content);
Sign up to request clarification or add additional context in comments.

1 Comment

@Matt: I did, but I'm concerned that the URL will still need things like ' ' (backticks don't work?) swapped with + and ' replaced with %27: I'm not really certain the best way to do those. Not with regex, I'm certain.
1

The key here is to use a non-greedy operator, and escape everything.

preg_replace('#\{google\}(.*?)\{/google\}#i','<a href="http://www.google.com?s=$1">$1</a>', $content);

2 Comments

@Matt since when? What's invalid about it?
Your curly brackets weren't escaped when I wrote my initial comment.
1

Your regex isn't valid. Try this:

preg_replace('#\{google\}[^\{]+\{/google\}#i', '<a href="http://www.google.com?s=$1">$1</a>', $content);

Comments

-1
preg_replace('#{google}([^{]+){\/google}#i', '<a href="http://www.google.com?s=$1">$1</a>', $content);

4 Comments

This is not a valid regex. The problem isn't the slash. OP uses # as the delimiter, so / doesn't need to be escaped. I'll remove my -1 if you can figure out which characters do need to be escaped.
Got me, didn't notice the '#' rather than '/', but how is it not valid? Taking a closer look, OP's original example works, so does mine for that example. Curly brackets would need to be replaced in certain scenarios, most obviously if he was using one for a delimiter or using them after a pattern in the right format. Curious to see if OP was trying to match a different subject. cdbconcepts.com/dev/reg.php
Curly braces are reserved characters. They must be escaped.
@Matt I agree they are reserved in certain situations, but for PCRE syntax do not need to be escaped unless attempting to match something in valid form. i.e. if you want to match subject a{,5} you must escape them a\{,5\}. However if you are matching alpha characters a{g} no need to escape. Also if your pattern used them as delimiters {test{curly{i you'd need to escape the one in the pattern.

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.