Solved

PHP displaying UTF-8 encoded characters

Posted on 2011-09-21
8
366 Views
Last Modified: 2012-05-12
So, this is probably a simple question, but I must be missing something. I successfully save UTF-8 encoded Chinese characters to a mysql database.

For example, they end up looking like this in the field ( This is random text taken from a google search, so I do not know what it means )
汉语/漢語

Open in new window


If I simply display it, it works fine. However, all my form values get htmlspecialchars treatment, and when this is done it ends up changing the & to & and displays the text as above and not as its corresponding Chinese character. There doesn't seem to be an additional step in any of the instruction I can find on dealing with these characters, so curious if I'm missing something simple.

I can of course "fix" it by replacing &# with &# after the htmlspecialchars call, I'd prefer to just know what I'm doing wrong though. Thanks!
0
Comment
Question by:WhistlingMtn
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:NUKIT
ID: 36573365
I belive the &amp is actually the '&' itself...

Have you tried just using: &27721;&35821;

?
0
 
LVL 13

Expert Comment

by:NUKIT
ID: 36573397
Yeah I just tried that within my Joomla installation.  The text editor is filtering the & and changing it to &.

The way you supplied the &#27721 into the content is how you can trick the system...

This is what I got using &27721;

&27721;

This is what I got using &#27721:
¿

The issue here is if you are using a text editor or not...as it's filtering your code and changing it on you...you can try turning it off and see what happens.

Looks like you are doing it right.
0
 

Author Comment

by:WhistlingMtn
ID: 36573413
well, & is the encoded version of &

I don't have a choice on what they're ending up as, they're getting encoded by mysql to UTF-8. The problem would still be the same though;
汉 and 汉 are not the same thing

<input type="text" value="&#27721;" /> Displays the Chinese Character
<input type="text" value="&amp;#27721;" /> Displays the literal "&#27721;" text

Open in new window


I can pick out the &amp;# and convert it back to &#, but having viewed examples online I didn't see anyone else requiring this, they just got their encoded text, htmlspecialchars, and display. Maybe I just misunderstood them.
0
 
LVL 13

Expert Comment

by:NUKIT
ID: 36573419
Whoa...even here they are using a text filter...it changed the character to an upside down question mark...here's a screen shot of what it looks like:

 char
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 13

Expert Comment

by:NUKIT
ID: 36573426
Ah, I see what you mean...
0
 

Author Comment

by:WhistlingMtn
ID: 36573430
I may just replace all &amp; back to &, since it's not a dangerous character in a text field anyway. Just perplexed as to why I'm having to do this when the dozens of threads online make no mention of it.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36595241
I am wondering about this part: all my form values get htmlspecialchars treatment -- why?  The usual place one might use htmlspecialchars() is to prevent user-supplied text from containing HTML markup in a message board or guest book.  Thus it would not apply to all form values, but would be used on external text before displaying the text output to the browser.  In any case, there are only five translations performed by the function, so you might try performing four of them yourself in a local function.
0
 

Author Closing Comment

by:WhistlingMtn
ID: 36595259
Yea I should have closed the question, this was basically my solution.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Page showing diff display 4 22
Upgrades AWS EC2 & RDS Boxes 1 12
Bootstrap 3 icons 3 15
Simple Angular 2 login page 4 14
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to count occurrences of each item in an array.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now