Let say we’ve text with more than one url within, so i would like to extract that url and doing some changes then shows up the text.
Here is the code i’m using
<?PHP
$text = "This is text with links http://google.com and www.yahoo.com";
$text = ereg_replace( "www\.", "http://www.", $text );
$text = ereg_replace( "http://http://www\.", "http://www.", $text );
$text = ereg_replace( "https://http://www\.", "https://www.", $text );
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if(preg_match($reg_exUrl, $text, $url)) {
$text = preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
echo $text;
}else{
echo "NO URLS";
}
?>
The problem
// output source (repeat for first link)
This is text with links <a href="http://google.com" rel="nofollow">http://google.com</a> and <a href="http://google.com" rel="nofollow">http://google.com</a>
It depends on array so it only takes the first URL $url[0] so how can i apply for all links to be found within the text ~ Thanks
Change the next line
to
and
to