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).

2
  • why are you using lang when you want to replace make ? Commented Apr 3, 2013 at 9:11
  • sorry that lang meant to be make. Commented Apr 3, 2013 at 9:16

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

both ways didn't work for me. the first way returned NULL and the second one returned strange output
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.
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);

1 Comment

if make comes directly after index.php?, it is not replaced. Also the values of all make='s are not removed as well
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;

1 Comment

if $url = "index.php?make=xx&ok=no" .. the outcome will leave extra & after index? .. which is not correct
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.