Jump to content

URL text to Hyperlink


miob

Recommended Posts

hello !

 

here is what i have

 

$threadx = preg_replace("/((ht|f)tp:\/\/[^\s&]+)/", "<a href=\"$1\">$1</a>", $threadx);

$threadx = preg_replace("/(\\S+@\\S+\\.\\w+)/", "<a href=\"mailto:$1\">$1</a>", $threadx);

 

can someone tell me how to make it better,

this one completly screws up existing hyperlinks and <img src=""> tags...

need to fix it somehow...

thanx

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/70670-url-text-to-hyperlink/
Share on other sites

try something like this:

 

<?php
$string = // some rubbish
$pattern[0] = "%(http|https|ftp)(://.*?)%";
$pattern[1] = "%((http|https|ftp)(://.*?)(jpg|bmp|gif|jpeg|png))%";
$replacement[0] = "<a href=\"\$1\$2\">\$1\$2</a>";
$replacement[1] = "<img src=\"\$1\" />";
for($i = 0; $i <= 1; $i++)
{
$string = preg_replace($pattern[$i],$replacement[$i],$string);
}
?>

 

~ Chocopi

Link to comment
https://forums.phpfreaks.com/topic/70670-url-text-to-hyperlink/#findComment-355244
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.