Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Please show the code that you already have. –  Sanja Sep 29 '13 at 21:27
    
Don't explode() urls, rather use function parse_url(). –  Glavić Sep 29 '13 at 21:50
add comment

4 Answers

up vote 2 down vote accepted
$str = 'http://example.com/a/b.swf';
$str = str_replace('http://', 'http://cache.', $str);
share|improve this answer
add comment
$new_string = str_replace('http://example.com/', 'http://cache.example.com/', $orig_string);

?

share|improve this answer
    
but values comes from a variable dynamically? –  hakiko Sep 29 '13 at 21:25
    
$new_string = str_replace(array_keys($map), array_values($map), $orig_string); if $map=array('example.com/'=>'http://cache.example.com/',...); –  Lajos Veres Sep 29 '13 at 21:26
    
When you replace the complete string with http... then its equal if there paramters. –  Stony Sep 29 '13 at 21:26
add comment

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

share|improve this answer
add comment

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);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.