简体   繁体   English

如何使用PHP阅读unicode网址?

[英]How do I read a unicode url with PHP?

I have a unicode url: \\test.php?sText=Московский 我有一个Unicode网址:\\ test.php?sText =Московский

I would like to use the $_Get function to work with the value of sText. 我想使用$ _Get函数来处理sText的值。 The code I have for test.php is: 我拥有的test.php代码是:

<?php
$sVar = $_GET['sText'];

echo "Variable = $sVar";
?>

Problem is that the above is coming bach as: Variable = ?????????? 问题是上面的代码是:变量= ??????????

What do I need to do? 我需要做什么?

  • You need to return a UTF-8 content-type header with your test.php header('content-type: text/html; charset=utf-8'); 您需要返回一个带有test.php header('content-type: text/html; charset=utf-8');的UTF-8内容类型标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) 您需要保存对test.php UTF-8编码的文件(对此不确定(但是不确定)

Thanks for all the feedback. 感谢您的所有反馈。

I have tried all the examples but I still get back question marks, even after saving as utf-8. 我已经尝试了所有示例,但是即使保存为utf-8,我仍然会收到问号。

Is there a php.ini setting I might need to add/enable maybe to allow unicode support? 是否有可能需要添加/启用php.ini设置才能启用unicode支持?

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. 因此,该页面能够使用并显示unicode。 Is this a limitationof the $_GET function maybe? 这可能是$ _GET函数的限制吗?


EDIT 编辑

I have just tried running the page in Google Chrome and it is working perfectly. 我刚刚尝试在Google Chrome浏览器中运行该页面,它运行良好。 This leads me to think its an IE setting. 这使我认为它是IE设置。 But what? 但是呢 And how do I work around it? 我如何解决呢?

This works fine for me: 这对我来说很好:

$sVar = $_GET['sText'];

echo urldecode($sVar);

I've tried to use iconv() function to solve your problem(function's parameters can vary, depending on your system configuration): 我尝试使用iconv()函数解决您的问题(函数的参数可能会有所不同,具体取决于您的系统配置):

$get = iconv('CP1251', 'UTF-8', $_GET['text']);
echo 'Variable = '.$get; // Variable = Московский

You can read about iconv functions . 您可以阅读有关iconv函数的信息 I hope, it'll be useful for you. 希望对您有用。

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. 当记录未写入我的数据库时,我首先注意到我的编码问题,数据库和表都设置为utf-8,从而离开了php。

To make things simple I created a test php page to demonstrate the problem. 为简单起见,我创建了一个测试php页面来演示该问题。 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=Московский 当页面从url获取($ _GET)值时,加载测试页面时,我在url test.php?sText =Московский中使用了unicode参数

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. 我认为,对我来说,问题是IE如何互操作url,出于某种原因,它无法对其进行解码或将其识别为unicode。 This became aparrent when I launched the failing page im Chrome, and it worked perfectly. 当我在Chrome中启动失败的页面时,这变得很明显,并且效果很好。

I proceeded to use the following function to output the full url in Chrome and IE 我继续使用以下功能在Chrome和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: Internet Explorer 8回应了以下内容:

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

Chrome however, echoed an encoded url: Chrome却回显了编码的网址:

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 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). 如您所见,chrome正在自动编码url,PHP正在自动正确读取和解码url,大概是因为我将内容类型设置为UTF-8(感谢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. 我仍然不确定为什么IE会这样,但是它使我朝着在调用页面中编码url的方向发展。 I was calling the page in javascript so I tried the function encodeURIComponent() . 我正在用javascript调用页面,所以尝试了函数encodeURIComponent() Thankfully this worked and now im getting the same result in CHROME and IE. 值得庆幸的是,此方法有效,现在我在CHROME和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'); 长话短说,如果我使用encodeURIComponent()来编码调用URL,那么我确保将目标php页面的内容类型设置为header('content-type: text/html; charset=utf-8'); all unicode url variables are processed correctly. 所有unicode url变量均已正确处理。

Thanks again for all your help. 再次感谢你的帮助。 Craig 克雷格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM