If you want a flexible regex, this will do:
Code:
$html = '<iframe width=\'380\' height="230" src="http://www.youtube.com/embed/1T4XMNN4bNM" frameborder="0" allowfullscreen></iframe>';
$width = 400;
$height = 250;
$html = preg_replace('#(\swidth\s*=\s*["\']{0,1})\d+(["\']{0,1})#i', '${1}' .$width. '${2}', $html, 1);
$html = preg_replace('#(\sheight\s*=\s*["\']{0,1})\d+(["\']{0,1})#i', '${1}' .$height. '${2}', $html, 1);
This will work even if there is some white space around attributes, which is allowed in html, also it will work for single quotes, double quotes or no quotes at all around the numbers and of course it will work for any numbers. Don't worry about it being slow, regex is not that slow - the above conversion takes 0.00007 seconds!
Bookmarks