2012-02-22 30 views
0

我有丰富的文本字段(称为描述),它包含文本和图像分离图像进行修改

我尝试找出所有图像修改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; 

所以ð oesn't工作时,有不止一个图像

+0

的代码看起来不完整的 - 哪里是foreach循环的结束括号? – 2012-02-22 17:58:58

+0

在你的if中删除break,你会发现一个图像并停止循环。 – 2012-02-22 18:10:45

回答

1

这在最近的一个项目工作对我来说更多:

<?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); 
    } 
+0

感谢您的帮助,但是您是否知道如何在img值之后添加如下内容(即:&h = 50):'src =“timthumb.php?src = my_image&h = 50' – bklups 2012-02-23 10:55:56

+0

这就是这段代码所做的:)查看倒数第二行的最后部分:'src =“timthumb.php?h = 50&src =' – 2012-02-23 20:35:15