i tried a long time to decode a ü to %FC but nothing helped. I hope someone can help me.
Here is a example url with this code:
http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=M%FCnchen&noList=false
Best regards
Gimo
Here You can encode your URL like this way :
<?php
// Here to encode whole URL
$url = "http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=München&noList=false";
$encoded = urlencode($url);
echo "Encoded URL : ".$encoded."</br></br></br>";
$decoded = urldecode($encoded);
echo "Decoded URL : ".$decoded."</br></br></br>";
//Or If you want to just encode the certain required part so here you can do like this way
$url = "http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=M".urlencode("ü")."nchen&noList=false";
echo "URL With Required Encoded Part : ".$url;
?>
OUTPUT :
Encoded URL : http%3A%2F%2Fwww2.dasoertliche.de%2F%3Fform_name%3Dsearch_nat_ext%26vert_ok%3D1%26zvo_ok%3D1%26kgs%3D09162000%26rgid%3D%26buab%3D%26zbuab%3D%26buc%3D2247%26sst%3Dyes%26page%3D1%26action%3D43%26st%3DDrygalski-Allee%2B51%26choose%3Dtrue%26sfn%3Dyes%26image%3DFinden%26skw%3Dyes%26context%3D1%26book%3D96%26ci%3DM%C3%BCnchen%26noList%3Dfalse
Decoded URL : http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=München&noList=false
URL With Required Encoded Part : http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=M%C3%BCnchen&noList=false
Related
Was so awesome of the customer to type "%10" instead of "10%"
0_o
$PACKAGE_json_decode = json_decode(urldecode(($_POST['textarea']), true);
print_r($PACKAGE_json_decode); // LENGTH 0
foreach($PACKAGE_json_decode as $row){
}
ERROR:
"Message: Invalid argument supplied for foreach()"
How do I urldecode without causing the %10 to take on a different meaning when sent back via AJAX?
And decode seems to produce that square character I cannot paste here... you know ... looks sorta like "[]"
*The string needs to be the same for the client when they get it back - they save it with a % they want it back with a %.
- Any suggestions about replacing it?
The % character is used in URL encoding. Either you remove % from the front end before passing to the server or you deal the same in the server side.
You could encode your request send by Ajax directly, such as below:
$.ajax({
type:'POST',
dataType: 'json',
...
Or like this:
JSON.stringify('%10');
Inside your PHP, just json_decode() now, as below.
$PACKAGE_json_decode = json_decode($_POST['textarea']);
Like this, your %10 will become "%10", et voila!!!
Please use the following code and let me know where is it breacking ?
<?php
$url_encode = urldecode("sampleTextWith%");
echo '<b>Ecoded URL </b>'. $url_encode."<br>";
$obj = new StdClass();
$obj->text = $url_encode;
$encoded_json = json_encode($obj);
echo '<b>Encoded JSON </b>'. $encoded_json."<br>";
$decoded_json = json_decode($encoded_json);
echo "<b>Decoded JSON </b>";
print_r($decoded_json);
echo "<br>";
foreach($decoded_json as $row){
echo "<b>Row Value : </b>". $row."<br>";
}
?>
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.
I want to encode like what browser did,
For example. https://www.google.com.hk/search?q=%2520你好
Should be encoded as https://www.google.com.hk/search?q=%2520%E4%BD%A0%E5%A5%BD
I am using the following regex to encode URI without like rawurlencode encode ~!##$&*()=:/,;?+'
$url = 'https://www.google.com.hk/search?q=%2520你好';
echo preg_replace_callback("{[^0-9a-z_.!~*'();,/?:#&=+$#]}i", function ($m) {
return sprintf('%%%02X', ord($m[0]));
}, $url);
But this will return https://www.google.com.hk/search?q=%252520%E4%BD%A0%E5%A5%BD which has an extra 25.
How can I correctly encode the URL that user inputed without modifying original address?
You can simply use urlencode for this purpose. It will encode %2520你好 into %252520%E4%BD%A0%E5%A5%BD . Use the code below.
<?php
$url = 'https://www.google.com.hk/search?q=%2520'.urlencode("你好").'';
echo $url;
?>
I think this will give you your desired url
Im trying to send a query string in url
for ex : url : localhost/myfile.php?number=8777,+9822,+9883
in myfile.php when i give echo the query string :
echo $_REQUEST['number'];
output :
8777,9822,9883
but the expected output is :
8777,+9822,+9883
How can i display + sign also.
UPDATE :
actually that url is web request from the android/ios device app,
im providing webservice in php,
so android/ios developers are sending request with a querystring contains + sign
so how can i handle this situation?
+ is reserved. PHP is correct in translating an unencoded + sign to a space.
You can use urlencode() urldecode() for this.
The + must be submitted in PHP as the encoded value: %2B
You should then use urlencode() function to create that url:
<?php
var_dump($_GET['number']);
echo 'http://localhost/myfile.php?number='.urlencode('8777,+9822,+9883');
EDIT
If this is url that you receiving and cannot do anything with that you can use for example:
echo substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'],'=')+1);
and you will get
8777,+9822,+9883
Sorry, i dont know what you're trying to do, but here's a suggestion
// where base64_encode('8777,+9822,+9883') = ODc3NywrOTgyMiwrOTg4Mw
localhost/myfile.php?number=ODc3NywrOTgyMiwrOTg4Mw
// on myfile.php
echo base64_decode($_REQUEST['number']);
// this will output -> 8777,+9822,+9883
UPDATE ---------------------------
if you have no other choice , you can use this
<?php
// get URL query string
$params = $_SERVER['QUERY_STRING'];
// if you have $params = www.mydomain.com/myfile.php?number=9988,+9876,+8768
$temp = explode('=', $params);
echo $temp[1] .'<hr>';
// if you have $params = www.mydomain.com/myfile.php?number=9988,+9876,+8768&number2=123,+456,+789
$params2 = $_SERVER['QUERY_STRING'];
$temp3 = explode('&', $params2);
foreach($temp3 as $val){
$temp4 = explode('=', $val);
// # // $GET = $temp4[0]; // if you need the GET VALUES
$VALUE = $temp4[1];
echo $VALUE .'<br>';
}
?>
Hope this helps.... :)
all.
I have different behavior of function urldecode() in PHP 5.2.x. Especially you will be able to see it with Wikipedia as good example.
Firstly, my page where I have results of that function has meta:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
Than I'm using function:
$url = urldecode($url);
echo $url;
Here is example of $url variable:
http://ru.wikipedia.org/wiki/%D0%91%D1%80%D0%B5%D1%81%D1%82
It will be decoded good. Result: "Брест"
http://ru.wikipedia.org/wiki/%CC%EE%EB%EE%E4%E5%F7%ED%EE
It will not be converted good. Result: ���������, but should be "Молодечно".
What's wrong? Why? I'm tried to use all functions from function.urldecode.php at PHP web-site, but it didn't give me any successful results
Here is quick example of code to test in PHP:
<?php
$url = array();
$url[] = "http://ru.wikipedia.org/wiki/%D0%91%D1%80%D0%B5%D1%81%D1%82";
$url[] = "http://ru.wikipedia.org/wiki/%CC%EE%EB%EE%E4%E5%F7%ED%EE";
foreach ($url as $value) :
echo urldecode($value) . "<br/>";
endforeach;
?>
Thanks in advance!
Not sure where you've taken that url, but the correct utf-8 one for "Молодечно" is:
$url = 'http://ru.wikipedia.org/wiki/%D0%9C%D0%BE%D0%BB%D0%BE%D0%B4%D0%B5%D1%87%D0%BD%D0%BE';
echo urldecode($url);
Your one is cp1251 encoded
As said zerkms, the following url is cp1251 encoded. To convert it to UTF-8, just use this:
$url = 'http://ru.wikipedia.org/wiki/%CC%EE%EB%EE%E4%E5%F7%ED%EE';
echo iconv("Windows-1251","UTF-8",urldecode($url));
//output: Молодечно