How to remove string in url with preg_replace

#1

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

#2

Why not use str_replace?

1 Like
#3

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

#4

Thanks, I changed it to this:

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

and it works.

1 Like
#5

Try this:


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

?>