Ok I want to replace the height and width tag of an image. It comes in this form within the img tag: height="300" width="300" Code (markup): This here is the code that works to replace it with something else but I can't get it working with just one preg_replace. function img_modify($text) { $pattern = '(\bheight\b[\=\s][\".]*[a-zA-Z0-9\-]*"\s)'; $text = preg_replace($pattern, 'height="200"', $text); $pattern = '(\bwidth\b[\=\s][\".]*[a-zA-Z0-9\-]*"\s)'; $text = preg_replace($pattern, 'width="200"', $text); return $text; } Code (markup): Would be great if anyone could enlighten me!
<?php $img="<img src=\"images/pic.png\" width=\"300\" height=\"200px\" alt=\"My pic\">"; $pattern[0]="/width=\"[a-zA-Z0-9]+\"/i"; $pattern[1]="/height=\"[a-zA-Z0-9]+\"/i"; $replacement[0]="width=\"NEW-WIDTH\"";//set your new width $replacement[1]="height=\"NEW-HEIGHT\"";//set your new height $img=preg_replace($pattern,$replacement,$img); //$img is the new image markup: Eg: <img src="images/pic.png" width="NEW-WIDTH" height="NEW-HEIGHT" alt="My pic"> ?> PHP: Enjoy