Skip to main content

Needing to expand this regex URL/mail parser a bit further





function make_clickable($text)

{

$ret = ' ' . $text;

$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $ret);

$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a target=\"_blank\" href=\"http://\\2\" >\\2</a>'", $ret);

$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);

$ret = substr($ret, 1);

return($ret);

}









as well as make sure that other domains like these still work:







I am not very fluent in regex at all and I stole this script from somewhere on the internet.





I know there are limitations to regex and this may be one, but any help at all would be greeaatly appreciated. I also notice that this site is using some nice javascript to parse urls really nicely. It worked on every one of my "problem" domains except for the one with (). Can anyone show me where stackoverflow's JS parser is? I was unable to locate it.





One more question: I am doing this for a newspaper site (to automatically parse links that the authors may write in their stories, as well as automatic mailto email addresses). I am thinking that it might be better to use javascript and let each client machine render the links that way. But I also want it to be reliable, so cross browser issues and things like noscript may come into play that way. Any thoughts?


Comments

  1. Seems to me you want the starting (^|[\n ]) replaced with \b, and a zillion other problems... Possibly change: [^ \"\n\r\t<]* to (\w|\W(?=\w|$)) for the second one:

    preg_replace('#\b(www|ftp)\.(\w|\W(?=\w|$))+#ise', '<a target="_blank" href="http://\\0" >\\0</a>', $ret);


    ... but thats just to get you started... It is no easy matter, and I'm not willing to put in the time to make it more full proof ;)

    ReplyDelete
  2. There's no way to make your current approach standards-compliant, and I can't be bothered either. Since you are just asking for the blackbox/magic regex codez, a simple workaround would be a negative assertion:

    (?<![.?;:)])


    Add that in your regex right before the #ise, so it won't match those characters at the very end.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...