0

我有一个用于 preg_replace 的正则表达式,用于替换以斜杠 (/) 结尾的 url。但它最后也会用 .jpg 替换 url。对于 url,它可以正常工作,但不应替换 .jpg url。有人能帮助我吗?

protected function _rewriteUrls($sContent)
{
        $target      = $this->getConfig()->getConfigParam('sShopURL').$this->_getToxidLangSeoSnippet().'/';
        $source    = str_replace('.','\.',$this->_getToxidLangSource());
        $actual    = '%href="'.$source.'(?=.*?.html)%';
        $should    = 'href="'.$target;
        return preg_replace($actual, $should, $sContent);
}

此代码来自一个名为 TOXID 的 OXID 模块,用于将 OXID 与另一个系统(如 wordpress)结合起来。$sContent 应该包含来自 Wordpress 博客的任何 HTML。所以这基本上重写了 URL,所以看起来我在 OXID 商店内导航。如您所见,最初它的正则表达式中有 .html,但如果您有不同的 URL 模式,这将毫无用处。所以我把它改成了斜杠(/)。不幸的是,它还会更改 .jpg 的 URL。

这是 sContent 的示例数据:http: //pastebin.com/nTXAAhWq

4

2 回答 2

1
$actual    = '%href="'.$source.'(?=.*?/)"%'; //if $sContent = '.. href="my/path/" ...'
$should    = 'href="'.$target.'"';

$指定行尾,如果在属性的结束语音标记$sContent之前结束,则很有用href

$actual    = '%href="'.$source.'(?=.*?/)$%'; //if $sContent = 'href="my/path/'
于 2013-08-01T15:56:24.883 回答
0

根据您链接到的内容修改:

$actual = '%href="'.$source.'(?=.*?/")%';
于 2013-08-01T16:00:21.800 回答