简体   繁体   English

PHP:特殊字符转换

[英]PHP: Special Characters Conversion

I have this problem where I converted a special character to be put on the URL as a parameter using Javascript Ajax request and then reads it to PHP. 我遇到了这样的问题:我使用Javascript Ajax请求将特殊字符转换为URL上的参数,然后将其读取到PHP。 The character is "Ñ". 字符是“Ñ”。

In my javascript I put the parameter as escape('PiÑa') and is converted to "Pi%D1a" 在我的JavaScript中,我将参数设置为escape('PiÑa')并转换为“ Pi%D1a”

And when I read it in my php a diamond shape with a question mark is what will appear. 当我在php中阅读它时,带有问号的菱形将会出现。 Here is how I read it. 这是我的阅读方式。

   escape(message) // Message being the "Pi%D1a"

Like I said a weird character comes out that when I save it my database, postgreSQL, It gives out an error. 就像我说的那样,出现了一个奇怪的字符,即当我将其保存到数据库postgreSQL时,它会发出错误消息。 How do I fix this? 我该如何解决?

D1 is the ISO-8859-1 ("Latin-1") encoded form of the "Ñ" character. D1是“Ñ”字符的ISO-8859-1(“ Latin-1”)编码形式。
A "diamond shape with a question mark" ( ) is the Unicode Replacement Character. “带问号的菱形”(。)是Unicode替换字符。 Whenever you see one, it indicates that the browser/editor/whatever-is-interpreting-the-text is trying to interpret text as Unicode and is encountering a character that is not valid in the assumed Unicode encoding. 每当您看到一个字符时 ,它都表明浏览器/编辑器/正在解释的文本正在尝试将文本解释为Unicode ,并且遇到了在假定的Unicode编码中无效的字符。

In other words, the character is actually Latin-1 encoded but you're telling the browser it's (likely) UTF-8 encoded. 换句话说,该字符实际上是Latin-1编码的,但是您要告诉浏览器它(可能是)UTF-8编码的。 You have an encoding mismatch. 您的编码不匹配。 Either tell the browser the right encoding via a Content-Type: text/html; charset=XXX 通过Content-Type: text/html; charset=XXX告诉浏览器正确的编码Content-Type: text/html; charset=XXX Content-Type: text/html; charset=XXX header, or convert the character from Latin-1 to UTF-8 before working with it. Content-Type: text/html; charset=XXX标头,或者在使用字符之前将字符从Latin-1转换为UTF-8。

Have you tried using urldecode($message) ? 您是否尝试过使用urldecode($message)

%D1 is the URL encoded representation of Ñ. %D1是Ñ的URL编码表示。

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

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