Replace values ​​in query string URI

I have a part of the query string in which I want to make a replacement. I want to use preg_replace, but I seem to hang in the regex.

Can anybody help? What I need to replace is GET vars.

Here is the line:

bikeType=G&nikeNumber=4351
+3
source share
2 answers

PHP has a handy feature for parsing query strings: parse_str () . You can take a look at this or provide more details, as your question is not entirely clear.

+8
source

You can use parse_stras already mentioned.
Alternatively, if you want to return them to the query string, you can usehttp_build_query

Example:

parse_str('bikeType=G&nikeNumber=4351', $params);
$params['bikeType'] = 'F';
$params['nikeNumber'] = '1234';
echo http_build_query($params, '', '&'); 

Output

bikeType=F&nikeNumber=1234

, parse_str (, , ). , PHP . . , .

// somewhere in your code you assigned the current user role
$role = $_SESSION['currentUser']['role'];
// later in the same scope you do
parse_str('bikeType=G&nikeNumber=4351&role=admin');
// somewhere later you check if the user is an admin
if($role === "admin") { /* trouble */ }

: http_build_query, &. , &.

+4

Source: https://habr.com/ru/post/1766187/


All Articles