我有一个既有文字也有图片的字符串,但我想只使用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 :(得分:0)
$feed_desc = preg_replace('/(<)([img])(\w+)([^>]*>)/', '', $str,1);
答案 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">