Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove specific parameter from URL while preserving other parameters

Tags:

regex

php

I want remove a parameter from a URL:

$linkExample1='https://stackoverflow.com/?name=alaa&counter=1';
$linkExample2='https://stackoverflow.com/?counter=4&star=5';

I am trying to get this result:

  • https://stackoverflow.com/?name=alaa&
  • https://stackoverflow.com/?&star=5

I am trying to do it using preg_replace, but I've no idea how it can be done.

like image 313
Alaa Gamal Avatar asked Jun 11 '12 18:06

Alaa Gamal


1 Answers

$link = preg_replace('~(\?|&)counter=[^&]*~','$1',$link);
like image 152
Crayon Violent Avatar answered Nov 15 '22 10:11

Crayon Violent