Essentially I am trying to detect URLs in a string, so as I can hyperlink them.
I essentially produced the following function
[code=php]
$array=explode(‘ ‘,$string);
foreach($array as $key => $val){
if(substr($val, 0, 7)==’http://’){
$array[$key]='<a href=”‘.$val.'” target=”_blank”>’.$val.'</a>’;
}
}
$string=implode(‘ ‘,$val);
This works fine, until you factor in the various html tags placed within the string output from my CMS. If the link is next to, for example, a <p> or <br /> tag, then there is no space in between for the explode function to isolate just the link.
I have experimented with further exploding each array value around ‘<‘ and ‘>’ symbols, which seems like it should work (well, in my head, anyway) But it doesnt seem to be.
The code I now have is as follows…
[code=php]
$content2=explode(‘ ‘,$content3);
foreach($content2 as $key => $val){
$subcontent=explode(‘<‘,$val);
foreach($subcontent as $key => $val){
$subsubcontent=explode(‘>’,$val);
foreach($subsubcontent as $key => $val){
if(substr($val, 0, 7)==’http://’){
$subsubcontent[$key]='<a href=”‘.$val.'” target=”_blank”>’.$val.'</a>’;
}
}
$val=implode(‘x’,$subsubcontent);
}
$val=implode(‘x’,$subcontent);
}
$content=implode(‘ ‘,$content2);
However, now it does not detect any of the links.
Is anyone able to suggest why this is not working, and perhaps demonstrate how I might be better off doing it? Or, of course, if there is a better method than this?