Search code examples
phpyoutubeurl-rewritingstrip

Rewrite Youtube URL


I have some YouTube URLs stored in a database that I need to rewrite.

They are stored in this format:

$http://youtu.be/IkZuQ-aTIs0

I need to have them re-written to look like this:

$http://youtube.com/v/IkZuQ-aTIs0

These values are stored as a variable $VideoType

I'm calling the variable like this:

$<?php if ($video['VideoType']){
$echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" href=\"" . $video['VideoType'] . "\">View Video</a>";
$}?>

How do I rewrite them?

Thank you for the help.


Solution

  • You want to use the preg_replace function:

    Something like:

    $oldurl = 'youtu.be/blah';
    $pattern = '/youtu.be/';
    $replacement = 'youtube.com/v';
    $newurl = preg_replace($pattern, $replacement, $string);