$subject = '[img width=320 height=217]'; $find = '/\[img width=(\d*) height=(\d*)\]/im'; $replace = 'width=\1 and height=\2'; $result = preg_replace($find, $replace , $subject);