revolution14
revolution14

Reputation: 43

php preg_replace swap out height and width of iframe

I am trying to replace the height and width on an html iframe src upon being saved to a database. I have looked at the preg_replace function and PCRE expressions but can't work it out. Listed below is my code and sample input

$pattern1 = '/width="[0-9]*"/';
$pattern2 = '/height="[0-9]*"/';
$subject  = '<iframe src="http://player.vimeo.com/?title=0&amp;byline=0&amp;portrait=0" width="400" height="300" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';

$returnValue = $preg_replace(array($pattern1, $pattern2), array('width="200"','height="200"'), $subject);

Any help would be much appreciated!

Cheers folks!

Upvotes: 0

Views: 1908

Answers (2)

Seyeong Jeong
Seyeong Jeong

Reputation: 11028

You put $ in front of the function.

$returnValue = preg_replace(array($pattern1, $pattern2), array('width="200"','height="200"'), $subject);

Your script can be simplified with the following:

$returnValue = preg_replace('/(width|height)="[0-9]*"/g', '$1="200"', $subject);

Upvotes: 1

Kenaniah
Kenaniah

Reputation: 5201

Because you're dealing with HTML, I would suggest using PHP's DOM capabilities - http://php.net/manual/en/book.dom.php. Regex is rarely the answer when working with HTML.

Upvotes: 1

Related Questions