I have the following code which gets the URL parameter after the ? and displays it.
<?php
$qt = http_build_query($_GET);
echo $qt;
?>
But the problem i am having is that, whenever the URL is as follows:
http://example.net/blog?123
The output is as follows:
123=
How can i get rid of that "=" sign? I am parsing this into a mysql db, so the equal sign is not necessary.
Thank you!
P.S: I know that i need to sanitize the requests for security reasons, and i have that covered.
Use str_replace:
$qt = str_replace("=", "", http_build_query($_GET));
Just try this:
str_replace("=", "", $qt);
Related
I need one help. I need to fetch all data from query string using PHP but some special charcters like (i.e-+,- etc) are not coming. I am explaining my code below.
http://localhost/test/getmethod.php?name=Goro + Gun
Here I need to get the value assign to name using the below code.
<?php
$name=$_GET['name'];
echo $name;
?>
Here I am getting the output like Goro Gun but I need the original value i.e-Goro + Gun .Please help me to resolve this issue.
#subhra try this for this case name=Goro + Gun:
<?php
$nameArr = explode('=', $_SERVER['QUERY_STRING']);
$name = str_replace("%20", " ", $nameArr[1]);
echo $name;
?>
$_SERVER['QUERY_STRING'] - this will return you full query string
there is an external page, that passes a URL using a param value, in the querystring. to my page.
eg: page.php?URL=http://www.domain2.com?foo=bar
i tried saving the param using
$url = $_GET['url']
the problem is the reffering page does not send it encoded. and therefore it recognizes anything trailing the "&" as the beginning of a new param.
i need a way to parse the url in a way that anything trailing the second "?" is part or the passed url and not the acctual querystring.
Get the full querystring and then take out the 'URL=' part of it
$name = http_build_query($_GET);
$name = substr($name, strlen('URL='));
Antonio's answer is probably best. A less elegant way would also work:
$url = $_GET['url'];
$keys = array_keys($_GET);
$i=1;
foreach($_GET as $value) {
$url .= '&'.$keys[$i].'='.$value;
$i++;
}
echo $url;
Something like this might help:
// The full request
$request_full = $_SERVER["REQUEST_URI"];
// Position of the first "?" inside $request_full
$pos_question_mark = strpos($request_full, '?');
// Position of the query itself
$pos_query = $pos_question_mark + 1;
// Extract the malformed query from $request_full
$request_query = substr($request_full, $pos_query);
// Look for patterns that might corrupt the query
if (preg_match('/([^=]+[=])([^\&]+)([\&]+.+)?/', $request_query, $matches)) {
// If a match is found...
if (isset($_GET[$matches[1]])) {
// ... get rid of the original match...
unset($_GET[$matches[1]]);
// ... and replace it with a URL encoded version.
$_GET[$matches[1]] = urlencode($matches[2]);
}
}
As you have hinted in your question, the encoding of the URL you get is not as you want it: a & will mark a new argument for the current URL, not the one in the url parameter. If the URL were encoded correctly, the & would have been escaped as %26.
But, OK, given that you know for sure that everything following url= is not escaped and should be part of that parameter's value, you could do this:
$url = preg_replace("/^.*?([?&]url=(.*?))?$/i", "$2", $_SERVER["REQUEST_URI"]);
So if for example the current URL is:
http://www.myhost.com/page.php?a=1&URL=http://www.domain2.com?foo=bar&test=12
Then the returned value is:
http://www.domain2.com?foo=bar&test=12
See it running on eval.in.
Looking for how to get the complete string in a URI, after the away?to=
My code:
if (isset($_SERVER[REQUEST_URI])) {
$goto = $_SERVER[REQUEST_URI];
}
if (preg_match("/to=(.+)/", $goto, $goto_url)) {
$link = "<a href='{$goto_url[1]}' target='_blank'>{$goto_url[1]}</a>";
The original link is:
https://domain.com/away?to=http://www.zdf.de/ZDFmediathek#/beitrag/video/2162504/Verschw%C3%B6rung-gegen-die-Freiheit-%281%29
.. but my code is cutting the string after the away?to= to only
http://www.zdf.de/ZDFmediathek
You know the fix for this preg_match function to allow really every character following the away?to= ??
UPDATE:
Found out, that $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING'] is already cutting the original URL. Do you know why and how to prevent that?
try use (.*) to get all after to=
$str = 'away?to=dfkhgkjdshfgkhldsflkgh';
preg_match("/to=(.*)/", $str, $goto_url);
echo $goto_url[1]; //dfkhgkjdshfgkhldsflkgh
Instead of extracting the URL with regex from the request URI you can just get it from the $_GET array:
$link = "<a href='{$_GET['to']}' target='_blank'>{$_GET['to']}</a>";
http://zyx.com/abc.html?style=1876&price=2%2C1000&size=68
http://zyx.com/abc.html?price=2%2C1000&style=1876&size=68
The url can appear in any of the two form:
I want to remove the entire price=2%2C1000& from my url.
I tried this thread. But with no luck
How to do this?
Try like this :
$str = explode("price","http://zyx.com/abc.html?style=1876&price=1%2C1000%2C2%2C1000&size=68");
$removeable_str = explode("&", $str[1]);
unset($removeable_str[0]);
echo $str[0].join("&",$removeable_str);
Try that:
var s = "http://zyx.com/abc.html?style=1876&price=2%2C1000&size=68"
s.split("price=")[1].split("&")[0];
I need to be able to remove a URL from a variable, I'm wondering how i do this.
Example - Say my script returns http://www.example.com/file.php?id=1234 i need to be able to remove the http://www.example.com/file.php?id= bit, just leaving the id number. If anyone can help, it would be great :)
Something like this?
$var = 'http://www.example.com/file.php?id=1234';
$query = parse_url($var, PHP_URL_QUERY);
$query_components = parse_str($query);
$id = $query_components['id'];
You can use regular expressions:
preg_match("/id=(\\d+)/", $url, $matches);
$id = $matches[1];
Just use $id = $_GET['id'];.
See the docs.
And don't forget to validate and sanitize.
The "id" in this case is being sent to your script as a GET variable, therefore you would access it as follows:
$id = $_GET['id'];
If you mean to say that this URL is not yours to control, then you would do this instead:
print_r(parse_url($url)); // Then analyze the output.