I am trying to find email addresses in text posted by users to my online forum, and change them into Bulletin Board Code (BBCode). So for instance:
example@yahoo.com
would become:
[mail]exmaple@yahoo.com[/mail]
However I don't want to change email addresses that are already in BBCode. So for instance, I don't want:
[mail]exmaple@yahoo.com[/mail]
to become:
[mail][mail]exmaple@yahoo.com[/mail][/mail]
Therefore I need to add a negative lookbehind assertion to my regular expression to ensure that an email address is not preceeded by the characters [mail] (or just ]).
The PHP code I'm using is:
$pattern = '#(?<!])([a-zA-Z0-9_\-\.]*@\S+\.\w+)#';
$bbcode = '[mail]$1[/mail]';
preg_replace($pattern, $bbcode, $text);
The problem I have is that the negative lookbehind is only applied to the first character for the email address seeking sub-pattern. For example when applied to the text:
[mail]example@yahoo.com[/mail]
The result is:
[mail]e[mail]xample@yahoo.com[/mail][/mail]
So the negative lookbehind finds [mail]e, but the rest of the email address xample@yahoo.com still validates. I reaise that this is because of the way the email address seeking sub-pattern is written, because it allows for any number of characters before the @.
How can I change the regular expression to make the negative lookbehind appliy to the whole of the email address seeking sub-pattern, whilst still successfully catching the majority of email addresses being posted?