0

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!

revolution14
  • 43
  • 1
  • 7

2 Answers2

1

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.

Kenaniah
  • 5,171
  • 24
  • 27
1

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);
Seyeong Jeong
  • 10,658
  • 2
  • 28
  • 39