0

更新:事实证明,以下是由我的生产服务器上的缓存问题引起的。感谢所有提供周到答案的人。

我在一个 php 页面上有一个简单的函数,它需要一个 url,例如:

http://myurl.com/mypage.html?param1=value1

并将其转换为:

http://myurl.com/searchpage.html?param1=value1

它所做的一切就是换掉 page.html 部分。

为此,我使用以下内容:

$currentUrl = $this->getCurrentUrl(); // 抓取当前 url,即 'http://myurl.com/mypage.html?param1=value1'

// 从当前 url 派生一个搜索模式
$模式=“/”。str_replace(array("/", ".", "-"), array("\\/", "\\.", "\\-"), $currentUrl) 。"/";

// 去掉'mypage.html'
$newUrl = preg_replace($pattern, 'http://myurl.com/', $currentUrl);

// 用正确的页面替换问号
$newUrl = str_replace("/?", "/searchpage.html?", $newUrl);

上面的代码不是确切的代码,而是一个很好的表示。它在一台服务器上运行良好,但是当我推送到生产环境时,preg_replace 不起作用。我最初尝试使用 str_replace。它也适用于我的本地开发机器,但不适用于生产服务器。

我已确认 URL 变量输入正确。有任何想法吗?

4

2 回答 2

2

这太令人费解了(很抱歉)。为什么不只是:

$newUrl = preg_replace('!\bmypage\.html\b!', 'searchpage.html', $oldUrl);
于 2010-04-01T23:57:16.883 回答
0

你为什么不做

$pieces = explode('/',$url);
str_replace('mypage','searchpage',$pieces[2]);
$newURL = implode('/',$pieces);

比使用正则表达式更好。

于 2010-04-02T00:15:06.797 回答