Greek characters encoding works in HTML but not in PHP

13,410

Even though it sounds really strange that your mysql data is outputted correctly wheres php strings fail on encoding, the way i would try to solve your problem, would be to break down the issue into steps trying to identify where this miscoding is generated!

First of all, you should try set your default_charset to utf-8 within the php.ini file, which is done like this :

default_charset = "utf-8";

If you can't do that because of provider restrictions you may still be able to change the value at runtime using the ini_set function!

You'll also want to make sure that the webserver is set to output utf-8 encoded files as well! In Apache this can done both in the httpd.conf or using htaccess files :

AddDefaultCharset UTF-8

At this point if everything fails, still... try to go with php headers and relative html charset :

<?php header("content-type: text/html;charset=utf-8") ?>
<!doctype>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <?php echo "α β γ δ ε ϝ ϛ ζ η θ ι κ λ μ ν ξ ο π ϟ ϙ ρ σ τ υ φ χ ψ ω ϡ" ?>
    </body>
</html>

It's very important though, that your files are saved using an appropriate encoding too (utf-8 is almost always the better choice, it helps to prevent issues quite a lot). If you saved files with encoding different than utf-8, destroy them a create new ones out of their old content. Sometimes editors aren't really able to switch encodings properly once the file is created, even though notepad++ generally does well on that; just use converto to not the encode in feature!

If it is still not working, although i hope it does by now, you may check out some other php alternatives like mb_detect_encoding, mb_convert_encoding, htmlentities and htmlspecialchars in order to fix the problem!

Share:
13,410
user969068
Author by

user969068

Updated on June 05, 2022

Comments

  • user969068
    user969068 almost 2 years

    I have very strange issue which cant figure out. i am using notepad++ and if i save a file as .php with Greek characters ( characters not from database ) it display Greek characters as question marks in web browsers but if i save same characters file as .html it display characters correctly.

    if Greek characters are displayed from database it show them correctly without issue. Also it is not working correctly on my shared hosting but works fine on my localhost.

    i tried to save .php file in different encoding but still same issue. I also tried to add php header() and meta tags with utf-8 but no luck.

    what could be the issue? Thanks

    • Pekka
      Pekka about 11 years
      The encoding that you save the file in must match the encoding you're declaring (e.g. through a Meta tag). If you make sure those match, it should work. If it still doesn't, can you show an example here and check your browser which encoding it's actually using for that page?
    • Pekka
      Pekka about 11 years
      I don't have access to a hex editor right now, but these ?? look like normal question marks to me. Are you sure you are saving proper greek characters in Notepad++ and the file is encoded UTF-8? That seems almost impossible. Can you go back to the .html file and save it as Php again? This looks like your characters were garbled somewhere along the way
    • Funk Forty Niner
      Funk Forty Niner about 11 years
      Have you tried saving in Notepad++ with BOM and without BOM?
    • user969068
      user969068 about 11 years
      yes Fred, i tried both but not luck.
    • Funk Forty Niner
      Funk Forty Niner about 11 years
      I tried it (from your source) WITH BOM and it shows up fine. It's all messed up if I save it WITHOUT BOM. Be careful, in Notepad++, you have to choose "convert to" and not "encode in" from the "encoding" menu, and save the file.
    • user969068
      user969068 about 11 years
      a Fred thanks you are life safer i tried convert method and works fine :) thanks again.
    • Funk Forty Niner
      Funk Forty Niner about 11 years
      It has to be a server issue or the way you're saving your .PHP file. When I copied the WITHOUT BOM as an .html file extension, and kept it as WITHOUT BOM, it shows being all messed up.