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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.