We help IT Professionals succeed at work.

Converting text url into link

Hi,

I have a text filed in my database that contain text and URLs. I want the URLs to be real links when that text is printed on page.

See the image attached to see it printed on page currently.

I want to make possible that those URLs be automatically clickable instead of text only.

Is this possible ?

text.jpg
Comment
Watch Question

Greg AlexanderLead Developer

Commented:
You can run the text through a function to convert them to urls, here is an example:

http://www.9lessons.info/2009/09/urls-as-links-with-regular-expressions.html
It would be helpful to know what database is this in and how are you outputting it to print.

Author

Commented:
Galexander, that is exact what I am looking for, it works great but I get a warning regarding a deprecated function:

Deprecated: Function eregi_replace() is deprecated in

Any idea how to replace that function ?

Thank you
<?php
function To_Link($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1">\\1</a>', $text);
$text = eregi_replace('(((f|ht){1}tps://)[-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;
}
?>

Open in new window

Greg AlexanderLead Developer

Commented:
Sorry, you should use  preg_replace() instead
Greg AlexanderLead Developer

Commented:
Here is a simpler example using preg_replace:

<?
$pattern = "@\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,%#-]+)*/?)@";
$text_raw = "here is a link http://google.com";
$text = preg_replace($pattern, '<a href="\0">\0</a>', $text_raw);
echo $text;
?>

Open in new window

Greg AlexanderLead Developer

Commented:
Here is the function version

<?php
function text_to_links($text){
	$pattern = "@\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)"
		."?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]"
		."{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,"
		."%#-]+)*/?)@";
		
return preg_replace($pattern, '<a href="\0">\0</a>', $text);
}

echo text_to_links('here is a link http://google.com');
?>

Open in new window

Author

Commented:
The original function is working better. I tried replacing eregi_replace by preg_replace but I get: Warning: preg_replace() [function.preg-replace]: Unknown modifier '('

I don't like the second solution because it will link also google.com while the first example link only texts with http:...

Any idea?
Greg AlexanderLead Developer

Commented:
I converted it to preg_replace:

<?php
function To_Link($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text = preg_replace('*(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)*',
'<a href="\\1">\\1</a>', $text);
$text = preg_replace('*(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)*',
'<a href="\\1">\\1</a>', $text);
$text = preg_replace('*([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)*',
'\\1<a href="http://\\2">\\2</a>', $text);
$text = preg_replace('*([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})*',
'<a href="mailto:\\1">\\1</a>', $text);
return $text;
}
?>

Open in new window

Greg AlexanderLead Developer

Commented:
That work for ya?

Author

Commented:
Hi galexander, sorry for the delay with reply:

Now it is closer but it breaks if there is a "(" in the url. Please run the php code attached and you will see.

Also, how to have every url in a new line ? Now it is printing all urls one after one, please check.

thank you.
<?php
function To_Link($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text = preg_replace('*(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)*',
'<a href="\\1">\\1</a>', $text);
$text = preg_replace('*(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)*',
'<a href="\\1">\\1</a>', $text);
$text = preg_replace('*([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)*',
'\\1<a href="http://\\2">\\2</a>', $text);
$text = preg_replace('*([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})*',
'<a href="mailto:\\1">\\1</a>', $text);
return $text;
}

$mylinks ="http://uploading.com/files/eb77bd97/The+Risk+Takers+%25281593155875%2529.epub

http://www.filesonic.com/file/2948278805

http://www.wupload.com/file/1878411146/The_Risk_Takers_(1593155875).epub

http://filepost.com/files/ef9d47m9/The%20Risk%20Takers%20(1593155875).epub";

echo To_Link($mylinks);
?>

Open in new window

Author

Commented:
Bump! Any idea please !

The fuction provided by galexander is working but URL breaks is there is a "+" " " or "_". See image attached.

Help.


break.jpg

Author

Commented:
Correction: the "_" character is not the one breaking the link, I think is the "(" "+" and white space: " "
Lead Developer
Commented:
Sorry for the delay myself! try this

<?php
function To_Link($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text = preg_replace('*(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=()]+)*',
'<a href="\\1">\\1</a>', $text);
$text = preg_replace('*(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=()]+)*',
'<a href="\\1">\\1</a>', $text);
$text = preg_replace('*([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=()]+)*',
'\\1<a href="http://\\2">\\2</a>', $text);
$text = preg_replace('*([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})*',
'<a href="mailto:\\1">\\1</a>', $text);
return $text;
}

$mylinks ="http://uploading.com/files/eb77bd97/The+Risk+Takers+%25281593155875%2529.epub

http://www.filesonic.com/file/2948278805

http://www.wupload.com/file/1878411146/The_Risk_Takers_(1593155875).epub

http://filepost.com/files/ef9d47m9/The%20Risk%20Takers%20(1593155875).epub";

echo To_Link($mylinks);
?>

Open in new window