2

I have a link in format like

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

I want to to convert it to

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

How can I do it?

I tried it with PHP's explode() function, but when I explode some part of string, then I add it to itself it does not work.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
hakki
  • 6,181
  • 6
  • 62
  • 106

4 Answers4

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

?

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
3
$str = 'http://example.com/a/b.swf';
$str = str_replace('http://', 'http://cache.', $str);
zkanoca
  • 9,664
  • 9
  • 50
  • 94
2

If you want to be more "professional", then use a special function http://php.net/manual/en/function.parse-url.php to parse URL.

Alex
  • 1,605
  • 11
  • 14
0

Try str_replace: str_replace ($search , $replace , $subject [, int &$count ])

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

$newstring = str_replace($substr, $substr.$attachment, $str);
Sage
  • 15,290
  • 3
  • 33
  • 38