I use this code to make urls clickable as anchor links in forum posts.
function makelink($str) {
$str = preg_replace_callback('/((((http)(s)?:\/\/)|www\.)[-0-9æøåa-zA-Z?-??-?\(\)%_+\.~#?&;:@\/\/=]+)(?<!\.)/i', function($matches) {
if (strtolower(substr($matches[0], 0 , 4)) == 'www.') {
$matches[0] = 'http://' . $matches[0];
}
return '<a href="'.$matches[0].'" title="link" rel="noreferrer">'.$matches[0].'</a>';
}, $str);
return trim($str);
}
It works fine. Now I need to also make youtube links into embed codes underneath the link (appended to the link I guess).
It's ok for this that there is an extra replacement routine going on.
How could I make some code that replaces the resulting anchor (if it's a youtube link):
<a href="https://www.youtube.com/watch?v=LOLAy72Tv24" title="link" rel="noreferrer">https://www.youtube.com/watch?v=LOLAy72Tv24</a>
With this:
<a href="https://www.youtube.com/watch?v=LOLAy72Tv24" title="link" rel="noreferrer">https://www.youtube.com/watch?v=LOLAy72Tv24</a>
<br />
<iframe class="youtube" width="350" height="250" src="https://www.youtube.com/embed/LOLAy72Tv24/" allowfullscreen></iframe>
So it just needs to take the video id and put out embed code underneath the original link, while outputting both together.
parse_url
to determine if the url host is YouTube and if so, append what you need.