0

In source code I have address's like this /images/image.jpg js/file.js how to add to this address's server address that will be http://server.com/folder/file.jpg with str_replace() it will be hard, because each folder name is different.

2
  • Probably easier to use IDE/text editor and search and replace.
    – ficuscr
    Commented Sep 20, 2012 at 15:26
  • please rate & accept the answer if it works for you. Commented Sep 20, 2012 at 15:56

2 Answers 2

1

Here is a solution using preg:

$content = '
  <script type="text/javascript" src="/js/script.js"></script>
  blah blah <a href="/images/image1.jpg">image1</a>
  blah blah <a href="/images/image2.jpg">image2</a>
  blah blah <a href="/folder/link1.htm">link1
';

$search = array('#"(/)images/#mi', '#"(/)folder/#mi', '#\"(/)js/#mi');
$replace = "http://site.com$1";

$content = preg_replace($search, $replace, $content);
1
  • this is only applicable when the entire content is generated in the controller, which is not a good MVC framework practice. Commented Sep 20, 2012 at 18:50
0

declare a global variable for your server address, store all your folder names into an array, and traverse it with a loop, and then concatenate the strings. Example: see it in action here

  $server_addr = "http://server.com";   
  //considering $replace and $folder_names have 1:1 correspondence of search and replace values 
 $orig = array("/images/image.jpg", "js/file.js"); //and so on...
 $replace = array("images", "js"); //and so on...
 $folder_names = array('folder'); //and so on...

 for( $i=0; $i<=count($replace); $i++) {
  foreach ($orig as $o)
   $new_path[] = $server_addr . str_replace($replace[$i], $folder_names[$i], $o);
}
6
  • At which point preg_replace is probably better than str_replace.
    – ficuscr
    Commented Sep 20, 2012 at 15:27
  • preg_replace is used to replace regular expressions, whereas str_replace is for strings. so in this case, a str_replace is the way to go inside the loop. Commented Sep 20, 2012 at 15:29
  • or write good regex and loop less? And why the looping anyway? Both functions accept an array for needle and haystack.
    – ficuscr
    Commented Sep 20, 2012 at 15:35
  • you need a loop coz there are multiple phrases that need to be searched in. Commented Sep 20, 2012 at 15:54
  • "If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search. The converse would not make sense, though. "
    – ficuscr
    Commented Sep 20, 2012 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.