Problem: authors have added email addresses wrongly in a CMS - missing out the 'mailto:' text.
I need a regular expression, if possible, to do a search and replace on the stored MySQL content table.
Cases I need to cope with are:
- No 'mailto:'
- 'mailto:' is already included (correct)
- web address not email - no replace
- multiple mailto: required (more than one in string)
Sample string would be: (line breaks added for readability)
<a href="[email protected]">[email protected]</a> and
<a href="mailto:[email protected]">[email protected]</a> and
<a href="http://www.test.com/">real web link</a>
second one to replace <a href="[email protected]">[email protected]</a>
Required output would be:
<a href="mailto:[email protected]">[email protected]</a> and
<a href="mailto:[email protected]">[email protected]</a> and
<a href="http://www.test.com/">real web link</a>
second one to replace <a href="mailto:[email protected]">[email protected]</a>
What I tried (in PHP) and issues:
pattern: /href="(.+?)(@)(.+?)(<\/a> )/iU
replacement: href="mailto:$1$2$3$4
This is adding mailto: to the correctly formatted mailto: and acting greedily over the last two links.
Thanks for any help. I have looked about, but am running out of time on this as it was an unexpected content issue.
If you are able to save me time and give the SQL expression, that would be even better.