- preg_match_all: documentation ( source)
- str_replace: documentation ( source)
<?php
$text = "<p>Some string www.test.com with urls http://test.com in it http://www.test.com.</p>";
if(preg_match_all("/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/",$text,$urls)){
foreach($urls[0]as $url ){
$text = str_replace($url,'<a href="'.$url.'">'.$url.'</a>',$text);
}
}
echo $text;