Here is some code I've wrote to automatically turn URL's into links and automatically embed any video urls from youtube. I made it for a chat room I'm working on and it works pretty well. I'm sure it will work just fine for any other purpose as well like a blog for instance.
All you have to do is call the function "autolink()" and pass it the string to be parsed.
For example include the function below and then echo this code.
`
echo '<div id="chat_message">'.autolink($string).'</div>';
/****************Function to include****************/
<?php
function autolink($string){
// force http: on www.
$string = str_ireplace( "www.", "http://www.", $string );
// eliminate duplicates after force
$string = str_ireplace( "http://http://www.", "http://www.", $string );
$string = str_ireplace( "https://http://www.", "https://www.", $string );
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
$m = preg_match_all($reg_exUrl, $string, $match);
if ($m) {
$links=$match[0];
for ($j=0;$j<$m;$j++) {
if(substr($links[$j], 0, 18) == 'http://www.youtube'){
$string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>',$string).'<br /><iframe title="YouTube video player" class="youtube-player" type="text/html" width="320" height="185" src="http://www.youtube.com/embed/'.substr($links[$j], -11).'" frameborder="0" allowFullScreen></iframe><br />';
}else{
$string=str_replace($links[$j],'<a href="'.$links[$j].'" rel="nofollow" target="_blank">'.$links[$j].'</a>',$string);
}
}
}
return ($string);
}
?>
`