3

I wanted to remove all occurrences of specific pattern of a parameter from a URL using preg_expression. Also removing the last "&" if exist The pattern looks like: make=xy ("make" is fixed; "xy" can be any two letters)

Example:

http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&

After processing preg_replace, the outcome should be:

http://example.com/index.php?c=y&do=ms&r=k&p=7

I tried using:

$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?lang=..&?)/i', '', $url);

However, this did not work well because I have duplicates of make=xx in the URL (which is a case that could happen in my app).

Blender
  • 289,723
  • 53
  • 439
  • 496
mimidov
  • 45
  • 2
  • 6

5 Answers5

8

You don't need RegEx for this:

$url = "http://example.com/index.php?ok=no&make=ae&make=as&something=no&make=gr&";

list($file, $parameters) = explode('?', $url);
parse_str($parameters, $output);
unset($output['make']); // remove the make parameter

$result = $file . '?' . http_build_query($output); // Rebuild the url
echo $result; // http://example.com/index.php?ok=no&something=no
HamZa
  • 14,671
  • 11
  • 54
  • 75
2

You could try using:

$str = parse_url($url, PHP_URL_QUERY);
$query = array();
parse_str($str, $query);
var_dump($query);

This will return to you the query as an array. You could then use http_build_query() function to restore the array in a query string.

But if you want to use regexp:

$url = "index.php?make=ae&ok=no&make=ae&make=as&something=no&make=gr";
echo $url."\n";
$url = preg_replace('/\b([&|&]{0,1}make=[^&]*)\b/i','',$url);
$url = str_replace('?&','?',$url);
echo $url;

This will remove all make in the URL

Peter Adrian
  • 279
  • 1
  • 5
  • both ways didn't work for me. the first way returned NULL and the second one returned strange output – mimidov Apr 03 '13 at 09:28
  • I edited the post, in my test, the regex works well. In the first way, i forgot that parse_str has the array passed by reference, and does not return it. – Peter Adrian Apr 03 '13 at 09:41
0

with rtrim you can remove last &

$url = rtrim("http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&","&");
$url = preg_replace('~&make=([a-z\-]*)~si', '', $url);
mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
  • if `make` comes directly after `index.php?`, it is not replaced. Also the values of all `make=`'s are not removed as well – mimidov Apr 03 '13 at 09:20
0
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?make=[a-z]{2})/i', '', $url);
echo $url;
Dale
  • 10,384
  • 21
  • 34
  • if `$url = "index.php?make=xx&ok=no"` .. the outcome will leave extra `&` after `index?` .. which is not correct – mimidov Apr 03 '13 at 09:23
0

Just by using preg_replace

$x = "http://example.com/index.php?c1=y&make=yu&do1=ms&r1=k&p1=7&";

$x = preg_replace(['/(\?make=[a-z]*[&])/i', '/(\&make=[a-z]*[^(&*)])/i', '/&(?!\w)/i'], ['?','',''], $x);
echo $x;

And the result is: http://example.com/index.php?c1=y&do1=ms&r1=k&p1=7

Hope this will be helpful to you guys.

Mannu
  • 1