Regexp: Replace only in specific context

Posted by blinry on Stack Overflow See other posts from Stack Overflow or by blinry
Published on 2010-03-25T12:55:43Z Indexed on 2010/03/25 13:03 UTC
Read the original article Hit count: 333

Filed under:
|
|
|

In a text, I would like to replace all occurrences of $word by [$word]($word) (to create a link in Markdown), but only if it is not already in a link. Example:

[$word homepage](http://w00tw00t.org)

should not become

[[$word]($word) homepage](http://w00tw00t.org).

Thus, I need to check whether $word is somewhere between [ and ] and only replace if it's not the case.

Can you think of a preg_replace command for this?

© Stack Overflow or respective owner

Related posts about php

Related posts about regex