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

用斜杠结尾的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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配