Php 需要正则表达式来替换图像源的部分

Php 需要正则表达式来替换图像源的部分,php,regex,preg-replace,Php,Regex,Preg Replace,我想替换下面的图像源 与 意思是我想替换“http://www.fishingwithdon.com/wp-content/“与”http://www.test.com/test/" 但我要替换的字符串每次都不相同 任何帮助都将不胜感激 谢谢 乌梅什·库尔卡尼 $str = preg_replace( '~\bhttp://www\.fishingwithdon\.com/wp-content/~', 'http://www.test.com/test/', $str

我想替换下面的图像源

意思是我想替换“http://www.fishingwithdon.com/wp-content/“与”http://www.test.com/test/"

但我要替换的字符串每次都不相同

任何帮助都将不胜感激

谢谢

乌梅什·库尔卡尼

$str = preg_replace(
    '~\bhttp://www\.fishingwithdon\.com/wp-content/~',
    'http://www.test.com/test/',
    $str
);

但对你来说,一个简单的回答就足够了吗

preg_replace ('/^(http:).*(\/)$/', 'http://www.test.com/test/', $src)
这就应该做到了

$html=“”;
$html  = "<img src=\"http://www.fishingwithdon.com/wp-content/uploads/2006/06/drop_off_area.jpg\"/>";
preg_match_all('#<img[^>]*>#i', $html, $match);
if(count($match) > 0)
{
    foreach($match[0] as $image)
    { 
        $new = preg_replace('/http:\/\/www\.(.+)\.com\//', 'http://www.test.com/test/', $image);
        $html = str_replace($image, $new, $html);
    }
}
echo $html;
preg#u match_all('#]*>i',$html,$match); 如果(计数($match)>0) { foreach($match[0]作为$image) { $new=preg\u replace('/http:\/\/www\.(.+)\.com\/','http://www.test.com/test/“,$image); $html=str_replace($image,$new,$html); } } echo$html;
可以相应地修改以适应不同的url情况(非www、非.com、https与http)

您确定需要正则表达式吗?这还不够吗?它甚至说:

如果不需要替换规则(如正则表达式),则应始终使用此函数而不是preg_replace()

例如:

$replace_needle = 'http://www.fishingwithdon.com/wp-content/';
$replacement = 'http://www.test.com/test/';
$replace_haystack = 'http://www.fishingwithdon.com/wp-content/uploads/2006/06/drop_off_area.jpg';
$result = str_replace($replace_needle, $replacement, $replace_haystack);

如果字符串每次都不相同,那么您如何计划预测字符串将是什么?