我有一个字符串,并希望删除任何图像URL的URL,例如。包含一个.jpg结尾。PHP从字符串中删除URL如果URL包含特定字母
我能够通过preg_match_all和strpos从字符串中提取和分离图像URL,但现在我需要从字符串本身“删除”显示的图像URL(以便我可以将图像与字符串分开处理)
preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match);
foreach ($match[0] as $link){
$strpos = strpos($link, '.jpg');
if ($strpos !== false){
echo $link;
break;
}
}
输入
$string = 'This is a string with a URL http://google.com and an image URL http://website.com/image.jpg';
所需的输出:
$string 'This is the string with a URL http://google.com and an image URL';
$image = '<img src="http://website.com/image.jpg"/>';
Str_replace url'' –