1

I have a query string stored in a variable and I need to strip out some stuff from it using preg_replace()

the parameters I want to strip out look like this:

&filtered_features[48][]=491

As there will be multiples of these parameters in the query string the 48 and the 491 can be any number so the regex needs to essentially match this:

'&filtered_features[' + Any number + '][]=' + Any number

Anyone know how I would do this?

3 Answers 3

1
$string = '&filtered_features[48][]=491';

$string = preg_replace('/\[\d+\]\[\]=\d+/', '[][]=', $string);

echo $string;

I assume you wanted to remove the numbers from the string. This will match a multi-variable query string as well since it just looks for [A_NUMBER][]=A_NUMBER and changes it to [][]=

Sign up to request clarification or add additional context in comments.

Comments

0
$query_string = "&filtered_features[48][]=491&filtered_features[49][]=492";
$lines = explode("&", $query_string);
$pattern = "/filtered_features\[([0-9]*)\]\[\]=([0-9]*)/";
foreach($lines as $line)
{
    preg_match($pattern, $line, $m);
    var_dump($m);
}

Comments

0
/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/'

this will match first number in n1 and second in n2

preg_match_all( '/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/', $str, $matches);

cryptic answer will replace more than necessary with this string:

&something[1][]=123&filtered_features[48][]=491

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.