dongwei4444 2013-08-01 15:55
浏览 236

用斜杠结尾的url的正则表达式

I have a regex for preg_replace for replacing urls ending with a slash (/). But it also replaces urls with .jpg at the end. For urls it works fine, but it should not replace the .jpg urls. Can someone help me please?

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);
}

This code is from an OXID module called TOXID to combine OXID with another system like wordpress. $sContent should contain any HTML from a Wordpress blog. So this basically rewrites URLs so that it looks like I am navigating inside the OXID shop. As you can see, originally it has got .html in its Regex, but this is useless if you have different URL patterns. So I changed it to a slash (/). Unfortunately it also changes URLs for .jpg.

Here is sample data for sContent: http://pastebin.com/nTXAAhWq

  • 写回答

2条回答 默认 最新

  • doutao6330 2013-08-01 15:56
    关注
    $actual    = '%href="'.$source.'(?=.*?/)"%'; //if $sContent = '.. href="my/path/" ...'
    $should    = 'href="'.$target.'"';
    

    The $ specifies the end of the line, useful if $sContent ends before the href attribute's closing speech marks

    $actual    = '%href="'.$source.'(?=.*?/)$%'; //if $sContent = 'href="my/path/'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序