简体   繁体   中英

Regex for url ending with a slash

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

$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/'

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

$actual = '%href="'.$source.'(?=.*?/")%';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM