PHP Regular Expression – PHP

  php

Q(Question):

Hello,
I have clickable url function which convert text into clickable urls. All works fine except it fails in one condition. Here is the code


function make_clickable_urls($text) {
$text = preg_replace("/(?<!<a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $text );
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1<a href="http://\\2" target="_blank">\\2</a>', $text);
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
'<a href="mailto:\\1" target="_blank">\\1</a>', $text);
return $text;
}

This function fails when using it founds this type of text.. <a href="http://url.com">http://url.com</a>

some one told me to use "Put the "a href" part back in" I tried but not luck.

Please help me

A(Answer):

why do you use 3 successive RegEx replaces?

A(Answer):

to convert following types of URLs

http://www.url.com
www.url.com
bytme@bytes.com

A(Answer):

I’m with Dormilich. The easiest way (maybe not shortest or most efficient) is to simply have a regex replace for each different form of url someonw may enter. Alternatively, can you search for "[a-zA-Z0-9].[a-z](2,3)" and then find any preceeding text and then process it accordingly? All url’s have the domain name and .com/.net/etc so if you really want to be tricky that would be one way.

A(Answer):

I think you are not getting it.. these urls are in a document where I allow HTML however some users do not use html because they do not so they put 2 types URL mentioned above as well as email which i convert by using make_clickable_urls() this function works 100% except it fails on this condition if following text found in a document

This is long paragraph http://www.url.com, this is HTML url <a href="http://www.url.com">http://www.url.com</a>

it ignores the a href but it looks inside http://.…. and convert them to HTML this is my problem

A(Answer):

To take any random html and reliably detect un-linked URLs requires a full parsing engine. Simple regular expressions will always fail on complex cases like

<a href="http://example.com">this link has a <b>bold</b> word in it, and points to http://example.com</a>

Simple solution: strip_tags().

Tweak to fix your immediate problem:

$text = preg_replace('#(?<![">])((http|ftp)s?://[^<>\s]+)#i', '<a href="\\0" target="_blank">\\0</a>', $text );

It’s a fragile solution, but may be good enough. It makes urls into links as long as they’re not preceded by a " or >.

A(Answer):

pwfisher you are great.. this worked excellent..


$text = preg_replace('#(?<![">])((http|ftp)s?://[^<>\s]+)#i', '<a href="\\0" target="_blank">\\0</a>', $text );

Btw: There is no need of strip_tags 🙂

Thanks

LEAVE A COMMENT