0

Possible Duplicate:
Change single variable value in querystring

I'm retrieving the current URL:

http://example.com/?s=original&fq=category

and now I want to insert an additional argument for the s=

s=original+additional&fq=category

If anyone knows this off the top of their head it would be greatly appreciated!

EDIT:

This is in a PHP WordPress environment. I'm tempted to stick with PHP Regex because I'm not sure how to use jQuery/js

2
  • 2
    For what programming language? There are many solutions, and some don't even require regex if the lang supports a URL parsing function Commented Feb 1, 2012 at 17:59
  • Whoops, this is a php environment. see my edit. Commented Feb 1, 2012 at 18:04

4 Answers 4

5

A solution that does not explicitly require regular expressions:

$old   = 'http://example.com/?s=original&fq=category';
$parts = parse_url($old);

parse_str($parts['query'], $query);
$query['s'] .= ' additional';

$new = sprintf(
  '%s://%s%s?%s'
    , $parts['scheme']
    , $parts['host']
    , $parts['path']
    , http_build_query($query)
);

echo 'Old: ', $old, PHP_EOL, 'New: ', $new, PHP_EOL;

The above code outputs:

Old: http://example.com/?s=original&fq=category
New: http://example.com/?s=original+additional&fq=category
Sign up to request clarification or add additional context in comments.

3 Comments

Actually maybe you could shed some light on this (or I can move it to a new topic): After the &fq=category, there is a colon and the actual category is wrapped in quotes (in Mozilla) like so, :"Yogurt" . If I copy and paste the URL, the output is, :%22Yogurt%22 . Now, if I use the solution you provided, the copy and pasted URL output is, %3A\%22Yogurt\%22 . So I'm experience some inconsistency here. I think I'll move this to a new issue.
I came up with a temporary fix: $query['fq'] = stripslashes(urldecode($query['fq']));
That's odd; I'm not able to replicate, though I think I found the new issue you created, so I'll take a look.
3

PHP has builtin functions for parsing and rebuilding URLs:

$bits = parse_url($url);
$args = parse_str($bits['query']);

$args['s'] .= 'morestuff';

$bits['query'] = http_build_query($args);
$new_url = http_build_url($bits);

Comments

1
preg_replace($s, '/([?&]s=)([^&]+)/', '$1$2+additional');

Quick PowerShell test:

PS> $urls = 'http://example.com/?s=original&fq=category', 'http://example.com/?a=original&s=foo&fq=category'
PS> $urls -replace '([?&]s=)([^&]+)', '$1$2+additional'
http://example.com/?s=original+additional&fq=category
http://example.com/?a=original&s=foo+additional&fq=category

2 Comments

$1 refers to the existing search term?
$1 refers to whatever is matched by the first parenthesis. In this case this includes a ? or & and s=.
0

You can this preg_replace:

$url = 'http://example.com/?s=original&fq=category';
$regex = '~([&?]s=)([^&]*)(&|$)~i';
$url = preg_replace($regex, '$1$2+additional$3', $url);
var_dump($url);

OUTPUT:

string(53) "http://example.com/?s=original+additional&fq=category"

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.