How to remove string in url with preg_replace

Hi,

I’m trying to remove “amp/” for my url.

I have code like this:

<?php echo Mage::helper('core/url')->getCurrentUrl(); ?>

It outputs like this:
https://www.example.com/amp/
https://www.example.com/amp/some-url.html

I’ve been trying but so far everything I’ve tried gives me an error:

<?php $str = Mage::helper('core/url')->getCurrentUrl()); ?><?php echo preg_replace('amp', "", $str); ?>

Can someone show how to do this?
Thank you

Why not use str_replace?

1 Like

You need to put delimiters either side of your search pattern.
But I agree with @Gandalf preg_replace is overkill for a simple replace of a known string, keep it simple and use str_replace.

Thanks, I changed it to this:

<?php $str = Mage::helper('core/url')->getCurrentUrl(); ?><?php echo str_replace("amp/","","$str"); ?>

and it works.

1 Like

Try this:


<?php
$str = Mage::helper('core/url')->getCurrentUrl(); 
echo str_replace("amp/",'',$str); 

?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.