I my opinion you don't need lookahead at all. Try this:
$find = array(
'~\[url=([^]]+)]\[img]([^[]+)\[/img]\[/url]~i'
);
$replace = array(
'<a href="$1" target="_blank"><img src="$2" class="bbcodeimage img-polaroid" alt="[img]" /></a>'
);
Explanations:
First at all, I have changed the pattern delimiter to ~
, the goal of this change is to avoid to escape all literal /
in the pattern. Literals ]
don't need to be escaped outside a character class or inside a character class if (and only if) it is the first character.
A lookahead is not useful in this situation because a lookahead is only a check and matches nothing. Example a(?=bc)
will find a a
followed by bc
but will only match the a
. It is why lookaheads and lookbehinds are also called "zero width assertions".
pattern details:
~ # delimiter
\[url= # literal: [url=
( # open the first capturing group
[^]]+ # all characters except ] (one or more times)
) # close the first capturing group
] # literal: ]
\[img] # literal: [img]
( # open the second capturing group
[^[]+ # all characters except [ (one or more times)
) # close the second capturing group
\[/img] # literal: [/img]
\[/url] # literal: [/url]
~i # delimiter and i modifier
Note that I have choosen to use single quotes for the replacement string to avoid to escape all double quotes of the string (and because there is no reason to use double quotes, no variables, no
or \t
etc.).