tags:

views:

140

answers:

6

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?

A: 
echo $_GET['sText'] # not $_Get
lyrae
thanks for the correction.
Fly_Trap
+1  A: 
  • You need to return a UTF-8 content-type header with your test.php header('content-type: text/html; charset=utf-8');
  • You need to save you test.php UTF-8 encoded (not sure about this, but wont hurt anyways)
Philippe Gerber
Thanks Philippe, I will try that later.
Fly_Trap
He has a problem with **GET request** encoding. The 1st hint only changes the **response** encoding. The 2nd hint only changes the **source code** encoding.
BalusC
how do you want to know that? if the response encoding is wrong he can't check the request encoding by responding ...
Philippe Gerber
A: 

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.

Alex
+1  A: 

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?

Fly_Trap
A: 

Check this blog for all stuff you need to take into account to get UTF-8 seamlessly to work in PHP: http://developer.loftdigital.com/blog/php-utf-8-cheatsheet.

BalusC
A: 

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:

http://localhost/FirstProject/test.php?sText=??????????

Chrome however, echoed an encoded url:

http://localhost/FirstProject/test.php?sText=%D0%9C%D0%BE%D1%81%D0%BA%D0%BE%D0%B2%D1%81%D0%BA%D0%B8%D0%B9

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

Fly_Trap