I have a rich text field (called description) which containing texts and images
I try to isolate all the images to modify the src tag to add elements before and after
example : <img src="path/to/myimage.jpg" /> should become <img src="timthumb.php1src=path/to/myimage.jpg&h=50" />
I succeed when there’s just one image in my field, i use this :
$descriptionencodee = utf8_encode($description);
$parser = xml_parser_create();
xml_parse_into_struct($parser, $descriptionencodee, $values);
foreach ($values as $key => $val) {
if ($val['tag'] == 'IMG') {
$first_src = $val['attributes']['SRC'];
break;
}
$string = $description;
$replacement = '<img src="'timthumb.php?src='. $first_src .'&h=50" />';
// $replacement = '${1}<br/>';
$string = preg_replace("/(\<img\b[^>]*>)/", $remplacement, $string);
echo $string;
so it doesn’t work when there’s more than one image
This worked for me in a recent project: