Php 替换字符串或explode()函数

Php 替换字符串或explode()函数,php,string,replace,str-replace,Php,String,Replace,Str Replace,我有一个像这样格式的链接 http://example.com/a/b.swf 我想把它转换成 http://cache.example.com/a/b.swf 我怎么做 我用PHP的explode()函数尝试了它,但是当我分解字符串的某个部分时,然后将其添加到自身中,它就不起作用了 $new_string = str_replace('http://example.com/', 'http://cache.example.com/', $orig_string); ?如果你想更“专业”,

我有一个像这样格式的链接

http://example.com/a/b.swf
我想把它转换成

http://cache.example.com/a/b.swf
我怎么做

我用PHP的
explode()
函数尝试了它,但是当我分解字符串的某个部分时,然后将其添加到自身中,它就不起作用了

$new_string = str_replace('http://example.com/', 'http://cache.example.com/', $orig_string);

如果你想更“专业”,那么就使用一个特殊的函数来解析URL。

试试:
stru-replace($search,$replace,$subject[,int&$count])

$str = 'http://example.com/a/b.swf';
$str = str_replace('http://', 'http://cache.', $str);

但是值是动态地来自变量的?$new\u string=str\u replace(数组键($map)、数组值($map)、$orig\u string);如果$map=array(');用http替换完整字符串时。。。如果有参数,那么它是相等的。请显示您已经拥有的代码。不要
explode()
URL,而是使用函数。
$str = 'http://example.com/a/b.swf';
$substr = 'http://';
$attachment = 'cache.';

$newstring = str_replace($substr, $substr.$attachment, $str);