Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a specific part of a query string PHP

Tags:

string

regex

php

I use $_SERVER['QUERY_STRING'] to get the query sting.

A example would be a=123&b=456&c=789

How could I remove the b value from the query string to obtain a=123&c=789 where b can be any value of any length and is alpha numeric.

Any ideas appreciated, thanks.

like image 664
pondpad Avatar asked Jul 22 '10 12:07

pondpad


People also ask

How can I replace part of a string in PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

How can I replace one word with another in PHP?

Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.

How do I replace Substr?

Definition and Usage The substr_replace() function replaces a part of a string with another string. Note: If the start parameter is a negative number and length is less than or equal to start, length becomes 0. Note: This function is binary-safe.


1 Answers

A solution using url parsing:

parse_str($_SERVER['QUERY_STRING'], $result_array);
unset($result_array['b']);
$_SERVER['QUERY_STRING'] = http_build_query($result_array);
like image 183
SirDarius Avatar answered Oct 29 '22 14:10

SirDarius