Forum Moderators: coopster

Message Too Old, No Replies

How to identify hyperlinks in files

         

Jo555

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

10+ Year Member



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.

mcibor

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

WebmasterWorld Senior Member 10+ Year Member



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]

Jo555

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

10+ Year Member



Thanks mcibor, Merry Christmas!