When entering this url into the Browser: http://www.google.com/?q=ä
The sent url is actually http://www.google.com/?q=%C3%A4
I want to do the same conversion using Php - how to do that?
What I tried:
$url = 'http://www.google.com/?q=ä'; //utf8 encoded
echo rawurlencode($url);
//gives http%3A%2F%2Fwww.google.com%2F%3Fq%3D%C3%A4
$u = parse_url($url);
echo $url['scheme'].'://'.$url['host'].$url['path'].'?'.rawurlencode($url['query']);
//gives http://www.google.com/?q%3D%C3%A4
The above url ist just a simple example, I need a generic solution that also works with
http://www.example.com/ä
http://www.example.com/ä?foo=ä&bar=ö
http://www.example.com/Περιβάλλον?abc=Περιβάλλον
The answer provided here is not generic enough: How to encode URL using php like browsers do
Ok, took me some time, but I think I have the universal solution:
function safe_urlencode($txt){
// Skip all URL reserved characters plus dot, dash, underscore and tilde..
$result = preg_replace_callback("/[^-\._~:\/\?#\\[\\]@!\$&'\(\)\*\+,;=]+/",
function ($match) {
// ..and encode the rest!
return rawurlencode($match[0]);
}, $txt);
return ($result);
}
Basically it splits the string using URL reserved characters (http://www.ietf.org/rfc/rfc3986.txt) + some more characters (because I think the "dot" should be also left alone) and does rawurlencode() on the rest.
echo safe_urlencode("https://www.google.com/?q=ä");
// http://www.google.com/?q=%C3%A4
echo safe_urlencode("https://www.example.com/Περιβάλλον?abc=Περιβάλλον");
// http://www.example.com/%CE%A0%CE%B5%CF%81%CE%B9%CE%B2%CE%AC%CE%BB%CE%BB%CE%BF%CE%BD?abc=%CE%A0%CE%B5%CF%81%CE%B9%CE%B2%CE%AC%CE%BB%CE%BB%CE%BF%CE%BD
// ^ This is some funky stuff, but it should be right
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With