PHP str_replace and preg_replace work on one server but not another
- by retailevolved
I have a simple function on a php page that takes a url such as:
http://myurl.com/mypage.html?param1=value1
and converts it to:
http://myurl.com/searchpage.html?param1=value1
All it does it swap out the page.html portion.
To do this, I use the following:
$currentUrl = $this-getCurrentUrl(); // Grabs the current url, i.e 'http://myurl.com/mypage.html?param1=value1'
// Derive a search pattern from the current url
$pattern = "/" . str_replace(array("/", ".", "-"), array("\\/", "\\.", "\\-"), $currentUrl) . "/";
// get rid of the 'mypage.html'
$newUrl = preg_replace($pattern, 'http://myurl.com/', $currentUrl);
// replace the question mark with the correct page
$newUrl = str_replace("/?", "/searchpage.html?", $newUrl);
The above code is not the exact code but is a good representation. It works beautifully on one server, but when I push to production, the preg_replace does not work. I originally attempted to use str_replace. It also works on my local development machine, but not on the production server.
I have confirmed that the URL variables are coming in correctly. Any ideas?