dprichard
02-04-2010, 11:55 AM
I am trying to wrap my head around the preg_replace thing, but it is a little confusing to me. I need to replace the width and height in an string but it needs to be a wildcard. I have it working for the width, but when I do the same thing with the height for some reason it doesn't work. It just outputs the height at the original value. Any help would be greatly appreciated.
<?php
$string = '<object width="580" height="360">';
$patterns = '/\width=".*?"/';
$replacements = 'width="250"';
$newstring = preg_replace($patterns, $replacements, $string);
$patterns = '/\height=".*?"/';
$replacements = 'height="200"';
$finalstring = preg_replace($patterns, $replacements, $newstring);
echo $finalstring;
?>
This is echoing <object width="250" height="360"> so it replaces the width, but not the height.
<?php
$string = '<object width="580" height="360">';
$patterns = '/\width=".*?"/';
$replacements = 'width="250"';
$newstring = preg_replace($patterns, $replacements, $string);
$patterns = '/\height=".*?"/';
$replacements = 'height="200"';
$finalstring = preg_replace($patterns, $replacements, $newstring);
echo $finalstring;
?>
This is echoing <object width="250" height="360"> so it replaces the width, but not the height.