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?