Hi, I'm having a problem with altering data in which my script automatically turns e-mail addresses and webpages into clickable links mixed with a search function that highlights keywords.
Here's is the auto-link code for email:
$info = preg_replace('/([^\s\"?|<>]+@([^\s\"?|<>]+\.)+?[a-zA-Z]{2,4})/i','<a href="mailto:\\1">\\1</a>',$info);
and web addresses:
$info = preg_replace('/((http|ftp):\\/\\/[a-zA-Z0-9]+(\.[-a-zA-Z0-9]+)*\.[a-zA-Z]{2,4}(\/[^\s<>\[\]\|\"\\\\]+)*)[\s]/i','<a href="\1">\\1</a>',$info);
$info = preg_replace('/[\s]((http|ftp):\\/\\/[a-zA-Z0-9]+(\.[-a-zA-Z0-9]+)*\.[a-zA-Z]{2,4}(\/[^\s<>\[\]\|\"\\\\]+)*)/i','<a href="\1">\\1</a>',$info);
$info = preg_replace('/([\s])(www\.[-a-zA-Z0-9]+\.[a-zA-Z]{2,4})/i','\1<a href="http://\\2">\\2</a>',$info); //This one checks for ww.whatever.dom (no http: etc)
*note - had to change 'www' to 'ww' so the forum script wouldn't convert the URL. Also, some backslashes might be missing since I had to go in and double some of the slashes to get them to display on this board (I might have overlooked one or two). I know for a fact that the regexps all work unless they intercept each other.
and here is what I use to highlight keywords (note: $search can contain multiple keywords delimited by |):
$info = preg_replace("/(".$search.")/i",'<span style="color: red">\\1</span>',$info);
If a keyword matches part of an auto-link, the code will mess up the anchor tag and create unwanted output. I've tried several alterations, but none of them worked, so I just gave the simplest working code I used.
Also, suggestions for better regexps for the ones shown above are welcome (I just noticed that I could've used [[:alphanum:]] 😉). I just can't solve this one!
Thanks for at least looking into this.