Link to home
Start Free TrialLog in
Avatar of ngexperts
ngexperts

asked on

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.
Avatar of hernst42
hernst42
Flag of Germany image

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

ASKER

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?
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");

?>
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.
Any other ideas on this? I'm still at a loss as to what to do.

Thank You.
ASKER CERTIFIED SOLUTION
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
logudotcom,

Sorry, doesn't work. Characters are still not encoded properly. I tried both utf8_encode() and utf8_decode().
Any other ideas anyone?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
oh great... nice to see the solved status
Brilliant I'd been searching for this solution for hours!