In this post we are discussing about the ways in which we can remove a parameter from url
we can do it by using preg_replace
and regular expressions

The regular expression used for this is

/\bp\=[^&]+&?/

and the whole code will look like this

preg_replace('/\bp\=[^&]+&?/', "", $string)

Where string is the URL from which the parameter needs to be removed

See the examples

$str[] = 'p=123';
$str[] = 'p=123&bar=456';
$str[] = 'bar=456&p=123';
$str[] = 'abc=789&p=123&bar=456';

foreach ($str as $string) {
	echo preg_replace('/\bp\=[^&]+&?/', "", $string), "<br>";
}

and its out put will be

bar=456
bar=456&
abc=789&bar=456

Hope this gives you and idea about how to remove a parameter from url

Leave a Reply

Your email address will not be published. Required fields are marked *