Code snippets, tech tricks and other bits and bobs

« Back to blog

PHP PCRE Regex: Halving video size - object and embed

Still with the activity stream.

Say I want to take only the first pair of object tags from a post, and halve the size of the object before displaying it? This way I keep a post short if it has several objects, and keep the size of videos down until the user clicks the ‘view more’ button.

Sample HTML

<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/u9pTZ3fj4Sc&hl=en_GB&fs=1&" /><param name="allowFullScreen" value="true" /><embed src="http://www.youtube.com/v/u9pTZ3fj4Sc&hl=en_GB&fs=1&" type="application/x-shockwave-flash" width="425" height="344"></embed></object><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/eGz2ayRG-no&hl=en&fs=1" /><param name="allowFullScreen" value="true" /><embed src="http://www.youtube.com/v/eGz2ayRG-no&hl=en&fs=1" type="application/x-shockwave-flash" width="425" height="344"></embed>

PHP code

$needle = '<object\s+[^>]*width\s*=\s*(?:"([^"]+)")\s+[^>]*height\s*=\s*(?:"([^"]+)")>((<param\s+[^>]*>)*)<embed\s+[^>]*width\s*=\s*(?:"([^"]+)")\s+[^>]*height\s*=\s*(?:"([^"]+)")><\/[e]mbed><\/object>/i';
    preg_match($needle, $haystack, $result);
    print_r($result);

Where $haystack is our html string as above

Result

Array
    (
        [0] => <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/u9pTZ3fj4Sc&hl=en_GB&fs=1&" /><param name="allowFullScreen" value="true" /><embed src="http://www.youtube.com/v/u9pTZ3fj4Sc&hl=en_GB&fs=1&" type="application/x-shockwave-flash" width="425" height="344"></embed></object>
        [1] => 425
        [2] => 344
        [3] => <param name="movie" value="http://www.youtube.com/v/u9pTZ3fj4Sc&hl=en_GB&fs=1&" />
        [4] => <param name="allowFullScreen" value="true" />
        [5] => 425
        [6] => 344
        [7] =>  allowfullscreen="true" type="application/x-shockwave-flash" src="http://www.youtube.com/v/u9pTZ3fj4Sc&hl=en_GB&fs=1&"/
    )

Create new video tag

$half_size_video = '<object width="' . round($result[1] / 2) . '" height="' . round($result[2] / 2) . '">' . $result[3] . $result[4] . '<embed width="' . round($result[6] / 2) . '" height="' . round($result[7] / 2) . $result[8] . '"></embed></object>';
    echo $half_size_video;

Result

<object width="213" height="172">
        <param name="movie" value="http://www.youtube.com/v/u9pTZ3fj4Sc&hl=en_GB&fs=1&" />
        <param name="allowFullScreen" value="true" />
        <param name="allowFullScreen" value="true" />
        <embed src="http://www.youtube.com/v/u9pTZ3fj4Sc&hl=en_GB&fs=1&" type="application/x-shockwave-flash" width="213" height="172 allowfullscreen="></embed>
    </object>

embedembedembedembedembed

Why does it say ‘embed’ repeatedly there? I dunno! Posterous, you are preposterous at times…

Posted May 27, 2010
May 27, 2010
Dave Disaster said...
For a step-by-step breakdown of the regex in this post see http://blog.absolutedisaster.co.uk/breaking-down-the-regex