Reputation: 1846
I have a unicode url: \test.php?sText=Московский
I would like to use the $_Get function to work with the value of sText. The code I have for test.php is:
<?php
$sVar = $_GET['sText'];
echo "Variable = $sVar";
?>
Problem is that the above is coming bach as: Variable = ??????????
What do I need to do?
Upvotes: 3
Views: 3950
Reputation: 844
This works fine for me:
$sVar = $_GET['sText'];
echo urldecode($sVar);
Upvotes: 2
Reputation: 1846
I finally resolved this, thank you all for some good tips (see last paragraph of this post for quick answer).
I was doing a few things wrong that was making things difficult.
I first noticed my encoding problems when records were not getting written to my database, the db and tables were all set to utf-8 so that left the php.
To make things simple I created a test php page to demonstrate the problem. As the page was getting ($_GET) values from the url, when I loaded my test page, I did so with a unicode parameter in the url test.php?sText=Московский
The problem for me, I think, was how IE was interoperating the url, for some reason it couldn't decode its or recognise it as unicode. This became aparrent when I launched the failing page im Chrome, and it worked perfectly.
I proceeded to use the following function to output the full url in Chrome and IE
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
echo curPageURL();
?>
Internet Explorer 8 echoed the following:
Chrome however, echoed an encoded url:
As you can see chrome was encoding the url automatically, and PHP was automatically reading and decoding it correctly, presumably because I had set the content type to UTF-8 (thanks Philippe).
I still wasn't sure why IE was behaving like this but it pushed me in the direction to encode the url in the calling page. I was calling the page in javascript so I tried the function encodeURIComponent()
. Thankfully this worked and now im getting the same result in CHROME and IE.
So long story short, if I use encodeURIComponent()
for encoding the calling url an I ensure that I set the content type of the destination php page to header('content-type: text/html; charset=utf-8');
all unicode url variables are processed correctly.
Thanks again for all your help. Craig
Upvotes: 1
Reputation: 3190
I've tried to use iconv() function to solve your problem(function's parameters can vary, depending on your system configuration):
$get = iconv('CP1251', 'UTF-8', $_GET['text']);
echo 'Variable = '.$get; // Variable = Московский
You can read about iconv functions. I hope, it'll be useful for you.
Upvotes: 0
Reputation: 1846
Thanks for all the feedback.
I have tried all the examples but I still get back question marks, even after saving as utf-8.
Is there a php.ini setting I might need to add/enable maybe to allow unicode support?
Currently, for me anyway, the following code:
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
<?php
$sVarControl = "Московский";
$sVar = $_GET['sText'];
echo "Control = $sVarControl <br />";
echo "Variable = $sVar <br />";
?>
</body>
</html>
Produces the following output:
Control = Московский
Variable = ??????????
So the page is capable of working with and displaying unicode. Is this a limitationof the $_GET function maybe?
EDIT
I have just tried running the page in Google Chrome and it is working perfectly. This leads me to think its an IE setting. But what? And how do I work around it?
Upvotes: 1
Reputation: 17836
header('content-type: text/html; charset=utf-8');
Upvotes: 1