Viewed   56 times

I am given a page url like 'http://abc.com/test.php?a=1&b=2&c=3'. Now I have been told to change the value of b to 5 so that it becomes 'http://abc.com/test.php?a=1&b=5&c=3'.

i.e change from http://abc.com/test.php?a=1&b=2&c=3 to http://abc.com/test.php?a=1&b=5&c=3

Note: variable b here can refer to any name.

 Answers

1

Use

  • parse_url() to extract the query string from the URL

  • parse_str() to split the query string into an array

  • array_merge() to add a new array "b" => 5

  • http_build_query() to re-build a query string

  • The remaining parts from the first step (protocol, host, path...) to re-build the full URL or - if you have the HTTP pecl extension - a http_build_url() with HTTP_URL_JOIN_QUERY will alleviate much of the work.

Wednesday, October 26, 2022
1

You can have this .htaccess inside /project/ directory:

RewriteEngine On
RewriteBase /project/

# rewrite to folder/subfolder/
RewriteCond %{DOCUMENT_ROOT}/project/folder/subfolder/$1.php -f
RewriteRule ^(.+?)/?$ folder/subfolder/$1.php [L]

# rewrite to folder/
RewriteCond %{DOCUMENT_ROOT}/project/folder/$1.php -f
RewriteRule ^(.+?)/?$ folder/$1.php [L]
Saturday, September 17, 2022
 
2

You can try something like this:

$test = preg_replace(
    '~/w+/([w-]+)/?type=(w+)~i',
    '/$2/$1/',
    '/technic/k-700/?type=repair'
);
var_dump($test);

The result will be:

string(14) "/repair/k-700/"
Sunday, November 13, 2022
 
3

You want to do something like this:

as_list = df.index.tolist()
idx = as_list.index('Republic of Korea')
as_list[idx] = 'South Korea'
df.index = as_list

Basically, you get the index as a list, change that one element, and the replace the existing index.

Saturday, September 3, 2022
 
2

I'd use http_build_query, which nicely accepts an array of parameters and formats it correctly. You'd be able to unset the edit parameter from $_GET and push the rest of it into this function.

Note that your code has a missing call to htmlspecialchars(). A URL can contain characters that are active in HTML. So when outputting it into a link: Escape!

Some example:

unset($_GET['edit']); // delete edit parameter;
$_GET['pagenum'] = 5; // change page number
$qs = http_build_query($_GET);

... output link here.
Saturday, November 19, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :