Special characters display incorrectly when pulling from Wordpress database.

I am developing a website which uses Wordpress as its blog component. When writing a post with special characters (— for example), the special characters render correctly on the wordpress portion of the site. However, when I pull data from the Wordpress database to display on the homepage (which is independent of the Wordpress framework), the special characters display in a peculiar manner (diamond with ? in center). It appears that the character is not being encoded correctly.

I checked the encoding when in the wordpress section of the site, and both browser and the header of the page are using UTF-8. The same is true for the homepage of the site. I can actually get the special characters to display correctly on the homepage if I switch the browser encoding to ISO-8859-1.

How do I correct this problem? I have tried using htmlentities() and htmlspecialchars() on homepage, but this doesn't correct the problem.
ngexpertsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
in this case try utf8_encode of the string instead of html*
0
Loganathan NatarajanLAMP DeveloperCommented:
did you set encoding utf-8 like word press display page? please tell me if it doesn't work..
0
Loganathan NatarajanLAMP DeveloperCommented:
let us mbstring() function also to convert it those to display
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ngexpertsAuthor Commented:
Thanks for the advice hernst42. I tried using utf8_encode on the string in question, however, rather than converting to the correct special character, it strinps it out entirely. Any ideas what to try next?
0
Loganathan NatarajanLAMP DeveloperCommented:
you may also try ,

<?php

/* Detect character encoding with current detect_order */
echo mb_detect_encoding($str);


/* "auto" is expanded to "ASCII,JIS,UTF-8,EUC-JP,SJIS" */
echo mb_detect_encoding($str, "auto");

?>
0
ngexpertsAuthor Commented:
In response to the latest replies:

* I set the encoding to UTF-8 in the html just as in the wordpress page
* I tried to use mbstring but it is not a recognized function (what does this do?)
* I  tried to use mb_detect_encoding. It returns "ASCII" for posts without special characters. For posts with special characters it returns nothing.
0
ngexpertsAuthor Commented:
Any other ideas on this? I'm still at a loss as to what to do.

Thank You.
0
Loganathan NatarajanLAMP DeveloperCommented:
PLEASE TRY THIS,

<?php

echo  utf8_decode($str);

?>

surely this will help you...

utf8_decode()

Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ngexpertsAuthor Commented:
logudotcom,

Sorry, doesn't work. Characters are still not encoded properly. I tried both utf8_encode() and utf8_decode().
0
ngexpertsAuthor Commented:
Any other ideas anyone?
0
Loganathan NatarajanLAMP DeveloperCommented:
according to me, it might be problem with

1) UTF encoding problem

2) The content might not have encoded properly

3) mbstring() config might have missed... that all i want to say...

so,.. just see what could be done..,
0
ngexpertsAuthor Commented:
Solved issue. Looked through Wordpress DB code and noticed it was setting mysql connection to use utf8.

"SET NAMES 'utf8'"

Put this in the homepage connect code and now encodes special characters properly.
0
Loganathan NatarajanLAMP DeveloperCommented:
oh great... nice to see the solved status
0
ollielawsonCommented:
Brilliant I'd been searching for this solution for hours!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.