Welcome to WebmasterWorld Guest from 35.173.234.237

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to identify hyperlinks in files

     
6:03 am on Dec 22, 2006 (gmt 0)

New User

10+ Year Member

joined:Nov 14, 2006
posts:36
votes: 0


I want to identify all hyperlinks in a string and get the url's.
I rember there is such a function in PHP but can't find it now.
Is my memory wrong?

If no such functions in PHP, does anybody have a script to do that?
Thanks in advance for your help.

10:46 am on Dec 22, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 26, 2003
posts:1133
votes: 0


You can use regex:

$str = preg_replace_callback("#([\n ])([a-z]+?)://([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)#si", "bbcode_autolink", $str);
$str = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]*)?)#i", " <a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $str);

function bbcode_autolink($str){
$lnk=$str[3];
if(strlen($lnk)>30){
if(substr($lnk,0,3)=='www'){$l=9;}else{$l=5;}
$lnk=substr($lnk,0,$l).'(...)'.substr($lnk,strlen($lnk)-8);}
return ' <a href="'.$str[2].'://'.$str[3].'" target="_blank">'.$str[2].'://'.$lnk.'</a>';}

Merry Xmas!
Michal Cibor

[edited by: jatar_k at 4:41 pm (utc) on Dec. 22, 2006]

1:18 pm on Dec 22, 2006 (gmt 0)

New User

10+ Year Member

joined:Nov 14, 2006
posts:36
votes: 0


Thanks mcibor, Merry Christmas!