hapablap
hapablap

Reputation: 409

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

Upvotes: 0

Views: 1248

Answers (2)

Adi Inbar
Adi Inbar

Reputation: 12323

Modified based on the content you linked to:

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

Upvotes: 0

MDEV
MDEV

Reputation: 10838

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

Upvotes: 1

Related Questions