Jump to content

convert url to links


Guest

Recommended Posts

i'm trying to convert all URL to links

 

i'm currently using this code:

 

<?php
$str = "http://www.i-want-to-convert-this.com <br>
<a href=\"http://www.this-link-is-already-converted.com/i-dont-want-to-convert-it.html\">Don't convert this !</a>";

$str = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" rel=\"nofollow\">\\0</a>", $str); 

echo $str;

?>

 

but this code is also converting URL that are already linked with the <a href> tag

 

can someone help me to modify this code to convert only URL without the <a href> tag ??

 

thanks a lot!

Link to comment
https://forums.phpfreaks.com/topic/205963-convert-url-to-links/
Share on other sites

I'm not entirely that great with regex. But I think if you build a regex statement to find the structure of a url be it ftp, irc, mailto, http, https.. and the like.. or multiple regex types to run through for each specific type of structure. Then if found take that bit of the string and wrap a link tag around it. Its just a random thought off my head but im thinking regex is more likely your best route to go.

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.