2014-10-08 55 views
0

我有一个字符串,既有文字和图像,但我想删除第一个图像只使用php。提取第一个图像,并从字符串中删除第一个图像

$string = 'This is my test <img src="link_to_image1">, some other text. 
<img src="link_to_another_image" border="0">'; 
$str = preg_replace('/\<img src=\"[aA-zZ0-9\/\_\.]+\"\>/','',$string, 1); 
+0

我试图像这样在不同位置的图像,我得到的答案$ feed_desc =的preg_replace('/(做的东西在未来的情况下, <)([img])(\ w +)([^>] *>)/','',$ str,1); – 2014-10-08 10:43:50

回答

0
$feed_desc = preg_replace('/(<)([img])(\w+)([^>]*>)/', '', $str,1); 
0

使用回调做。可能是一个更灵活一点对你在你想要基于字符串

class imgReplacer { 

    function cb($matches){ 

     if (!$this->counter){ 

      $this->counter++; 
      return ''; 

     } else { 

      return $matches[0]; 

     } 

    } 

} 


$ir = new imgReplacer; 
$ir->counter = 0; 

$string = 'This is my test <img src="link_to_image1">, some other text. <img src="link_to_another_image" border="0">'; 

$string = preg_replace_callback(
    '#(<img.*?>)#', 
    array(&$ir, 'cb'), 
    $string); 

echo $string; 

This is my test , some other text. <img src="link_to_another_image" border="0"> 
相关问题