PHP str_replace and preg_replace work on one server but not another

Posted by retailevolved on Stack Overflow See other posts from Stack Overflow or by retailevolved
Published on 2010-04-01T23:55:02Z Indexed on 2010/04/02 0:03 UTC
Read the original article Hit count: 333

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about preg-replace