Soak blog

Get the lowdown on what we love, what we hate and everything in-between.

leenewell

11.03.2010

By: leenewell

Under: Uncategorized

Resizing Youtube Embed Video using PHP preg_replace

Below is a PHP function that takes in youtube embed code, a desired width and height. It returns the embed code with width and height resized to the desired dimensions.

This is very useful when uploading youtube videos using a CMS or allowing users to upload youtube videos as user generated content.

function resizeYoutubeEmbeddedVideo($embedCode, $width, $height) {
$patterns = array();
$patterns[0] = '/width="(.*?)"/';
$patterns[1] = '/height="(.*?)"/';
$replacements = array();
$replacements[0] = 'width="' . $width . '"';
$replacements[1] = 'height="' . $height . '"';

return preg_replace($patterns, $replacements, $embedCode);
}

4 Responses to “Resizing Youtube Embed Video using PHP preg_replace”

  1. Sarbagna says:

    Very helpful one…but for some reason it didn’t work for me. It return unchanged values. Did I miss anything?

  2. Sarbagna says:

    Plz ignore my previous post. This works like a charm. :)

    Thanks a ton.

    Best regards
    innIndia

  3. John says:

    Very nice! Just what I needed.

  4. Colonel says:

    Thanx for the script. It works like magic.

Leave a Reply