隔离要修改的图像


Isolate images to be modified

我有一个富文本字段(称为描述),其中包含文本和图像

我试图隔离所有的图像,以修改src标签,在之前和之后添加元素

示例:<img src="path/to/myimage.jpg" />应该变成<img src="timthumb.php1src=path/to/myimage.jpg&h=50" />

当我的领域只有一个图像时,我成功了,我使用这个:

            $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;

所以当有多个图像时它就不起作用了

这在我最近的一个项目中起到了作用:

<?php
    $content = $your_html_here;
    preg_match_all( '/<img .* '/>/iU', $content, $results );    // Find all <img />
    foreach ( $results[0] as $one_image ){ // Loop through results
        // Replace <img /> with altered <img />
        $content = str_replace( $one_image, str_replace( 'src="', 'src="timthumb.php?h=50&src=', $one_image ), $content );
    }