Alexis Alexis - 1 month ago 9
PHP Question

Cannot remove string

I have using this function to find if there is a specific string is there in the url if it is then replace it with '' and echo

<?php
$url= $_SERVER['REQUEST_URI'];
if (strpos($url, '&cpp') !== false)
{
$url=preg_match("/&cpp=(\d+)/",'',$url);
}
echo $url;
?>


And the $url is

http://localhost:9999/store/product/manageproducts.php?category_id=21&cpp=10&cpp=0


But it doesn't remove neither it shows any result

Answer

Use preg_replace instead of preg_match

I have done it and its working fine

<?php 
$url= 'http://localhost:9999/store/product/manageproducts.php?category_id=21&cpp=10&cpp=0'; 
if (strpos($url, '&cpp') !== false)
{
$url=preg_replace("/&cpp=(\d+)/",'',$url);
}
echo $url;
?>