Forum Moderators: coopster

Message Too Old, No Replies

convert text url to clickable urls

and leave html urls as is

         

phparion

8:13 pm on May 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi

I want to send text to a function which can make my text url clickable links.

I have found this function

PHP Code:
function makeClickableLinks($text) {

$text = eregi_replace('(((f¦ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',

'<a href="\\1">\\1</a>', $text);

$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',

'\\1<a href="http://\\2">\\2</a>', $text);

$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',

'<a href="mailto:\\1">\\1</a>', $text);

return $text;

}

it works fine for text urls but it destroys my html links in which i have give proper hyperlink tags values.

can anybody help with fixing this function so it makes text clickable urls but also dont disturb my html links?

thank you

coopster

9:20 pm on May 1, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would use the Perl Compatible Regular Expression engine and a negative lookbehind assertion [php.net].

phparion

4:04 am on May 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Coopster! mmauhhh!

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

the above worked for me like a charm using negative assertion.

coopster

7:32 pm on May 9, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That pattern looks as if it should work OK. Except it is going to fail you on image links. In that case, just drop the a href at the beginning:
$pattern = "/(?<!=\")((http¦ftp)s?:\/\/[^<>\s]+)/i";