How to replace a query string like
page=3&searchform=1&cityS=-1&category=24& to page=[0-9] ?
I'm using the following
preg_replace('/page=d*&?/','',$_SERVER['QUERY_STRING']);
but in the end, &3 appears.
You can use:
preg_replace('/^.*?(\bpage=[0-9]*)&.*$/','$1', $_SERVER['QUERY_STRING']);
Or else you can simply use
$_GET['page']
To get this query parameter value.
Related
I'm importing data into mysql using php from a csv. I want to replace empty date fields with a date captured in a variable. I thought I could use this but it's not working:
$appt_date=str_replace('', $alt_date, $appt_date);
Any ideas why this won't work?
str_replace() is for replacing substrings within a string with something else, not for matching the whole string. You should use an if statement.
if ($appt_date == '') {
$appt_date = $alt_date;
}
i'm trying to inject an extra param at the end of the query string of a url being passed as a value.
i have tried
<?PHP preg_replace('/(http:\/\/www.site.com)/i','$1',$_GET['url']."&EXTRA-PARAM");
but sometimes URL does not contain any query string. how can i make it start with ? instead of & if there is no query string presant
The preg_replace you have in your question has no effect:
preg_replace('/(http:\/\/www.site.com)/i','$1',$_GET['url']."&EXTRA-PARAM");
Whatever the last argument is, it will be returned by that preg_replace call. Either you don't have a match and nothing is replaced, or you have a match, but then you replace what you found with... what you found. So in both cases you get the same result.
$1 is a back-reference to http:\/\/www.site.com, so actually the quoted statement is equivalent to this:
preg_replace('/http:\/\/www.site.com/i','http:\/\/www.site.com',
$_GET['url']."&EXTRA-PARAM");
Your final value is always:
$_GET['url']."&EXTRA-PARAM"
So, you could just have used that, and skip the preg_replace.
What I answer here is how to better write that part ($_GET['url']."&EXTRA-PARAM"), i.e. the question: "inserting an extra param at the end of the URL".
This concatenation can lead to missing ?. Worse, it will not lead to a functional parameter if $_GET['url'] has a hash (#).
Here is a solution for that with preg_replace_callback. It takes into account that a given URL may or may not have:
a hash (#) component;
a question mark ?
some query after the ?
some & to separate query parts
The function accepts as second argument what needs to be added to the query part of the provided URL, which may be a string (must be URL encoded), or an associative array with items to add to the URL. This array will then be fed to the function http_build_query:
function addUrlQuery($url, $qry) {
if (!is_string($qry)) $qry = http_build_query($qry);
return preg_replace_callback("/^(.*?)(\?(.*?))?(#.*)?$/",
function ($matches) use ($qry) {
return $matches[1] . "?" .
(empty($matches[3]) ? "" : $matches[3] . "&") .
$qry .
(empty($matches[4]) ? "" : $matches[4]);
}, $url);
}
Example use:
$url = addUrlQuery($_GET['url'], "EXTRA-PARAM");
echo addUrlQuery("http://myserver.com?x#title", ["EXTRA-PARAM" => 1]);
echo addUrlQuery("http://myserver.com/" . $relative_url, "EXTRA-PARAM");
You can see the function run on several test cases on eval.in.
The problem with $_GET['url']
If you have a URL passed as a query argument to the current URL, like this:
http://myserver.com/index.php?url=http://test.com/index.php?a=1
Then you must realise this is not a good URL. Some characters need to be escaped. Certainly the ampersand (&) needs to be escaped. If you have this:
http://myserver.com/index.php?url=http://test.com/index.php?a=1&b=2
Ask yourself: is b a query parameter of the current URL, or of the URL passed as url argument?
The answer: it is a query parameter of the current URL. You would access it with $_GET['b']. And the value of $_GET['url'] is now just:
http://test.com/index.php?a=1
So... you need to escape & when you want b to be a query argument of the url parameter, and not of the current query:
http://myserver.com/index.php?url=http://test.com/index.php?a=1%26b=2
Note the %26: it is the code for &. Now if you look at $_GET['url'] you will get:
http://test.com/index.php?a=1&b=2
What if URL is provided without proper encoding?
In this case, you have to make assumptions. If you know for sure that the url parameter is the only parameter you will get on the current URL, then you can conclude that any other you get, really belong to the URL that is passed via that url parameter.
You would then take all the parameters from $_GET, except url, and add them (with the function above) to the value of $_GET['url'].
I have an URL like so:
http://local.mywebsite.com/index.php?p=edit_entry?id=3
and I want to retrieve only the p argument which value in this case above is "edit
_entry".
I have written:
$p = $_GET['p'];
echo $p;
But when I do so I get the value edit_entry?id=3
What should I do? I just want to retrieve edit_entry
That's an invalid URL. ? signifies the START of the query parameters, it's not a seperator BETWEEN parameters. You use & for that:
http://local.mywebsite.com/index.php?p=edit_entry&id=3
^---
Because the url should be:
http://local.mywebsite.com/index.php?p=edit_entry&id=3
^ ampersand
It should be & instead of ?
? is the delimiter which separates the URL from the PARAMETERS.
The parameters are separated by an ampersand &.
So you have to use index.php?param1=something¶m2=something&etc=etc
I have got a url like this - http://www.facebook.com/profile.php?id=1820816888
Now i want to get the value in the id - 1820816888
I tried on many ways they are giving whole part of the query - id=1820816888
I tried parse_url but it shows id=1820816888 but just want to get 1820816888
Use a regular expression, e.g.
$url = 'http://www.facebook.com/profile.php?id=1820816888';
preg_match('/.+[?&]id=(\d+)/', $url, $matches);
echo $matches[1];
all the parameters are stored in $_GET array:
so something like:
echo $_GET['id'];
will give you the value of the id.
I've this url:
http://localhost/alignment/?page_id=52&q=2
I want to get the portion: ?page_id=52 , how can I do that?
$_SERVER['QUERY_STRING']
or
$url = "http://localhost/alignment/?page_id=52&q=2";
$bits = explode("?", $url);
$querystring = $bits[1]; // this is what you want
but the first one will be much more reliable and is easier. :)
EDIT
if you meant that you just wanted that one variable use:
$_GET['page_id']
This is called a query string. The main portion of the query string is separated by the rest of the URL with a ?. Each argument in the query string is separated by a &.
PHP:
$_SERVER['QUERY_STRING'];
If you want to get the individual pieces, use:
$_GET['page_id']; //etc...
You can get the whole query string with $_SERVER['QUERY_STRING'], but you would have to parse out page_id part. If you insist on doing things manually the function parse_str may come in handy.
A better choice would be to just use the predefined $_GET global variable. $_GET['page_id'] would give you value 52.
If you have it as a string, use parse_url. Documentation here. Get the query value of it. or if its current request use $_SERVER['QUERY_STRING']
echo parse_url($url,PHP_URL_QUERY);
This should do it:
echo $_SERVER['QUERY_STRING'];
Assign the url to string and then explode() it,
http://php.net/manual/en/function.explode.php, using the ? as a delimiter