简体   繁体   中英

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. The character is "Ñ".

In my javascript I put the parameter as escape('PiÑa') and is converted to "Pi%D1a"

And when I read it in my php a diamond shape with a question mark is what will appear. 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. How do I fix this?

D1 is the ISO-8859-1 ("Latin-1") encoded form of the "Ñ" character.
A "diamond shape with a question mark" ( ) is the Unicode Replacement Character. 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.

In other words, the character is actually Latin-1 encoded but you're telling the browser it's (likely) UTF-8 encoded. 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 header, or convert the character from Latin-1 to UTF-8 before working with it.

Have you tried using urldecode($message) ?

%D1 is the URL encoded representation of Ñ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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