2

我有一个格式的链接,如

http://example.com/a/b.swf

我想将其转换为

http://cache.example.com/a/b.swf

我该怎么做?

我尝试使用 PHP 的explode()函数,但是当我分解字符串的某些部分时,我将它添加到自身,它不起作用。

4

4 回答 4

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

?

于 2013-09-29T21:24:32.137 回答
3
$str = 'http://example.com/a/b.swf';
$str = str_replace('http://', 'http://cache.', $str);
于 2013-09-29T21:25:53.023 回答
2

如果你想更“专业”,那就使用一个特殊的函数http://php.net/manual/en/function.parse-url.php来解析 URL。

于 2013-09-29T21:25:16.523 回答
0

尝试str_replacestr_replace ($search , $replace , $subject [, int &$count ])

$str = 'http://example.com/a/b.swf';
$substr = 'http://';
$attachment = 'cache.';

$newstring = str_replace($substr, $substr.$attachment, $str);
于 2013-09-29T21:34:18.013 回答