Not displaying Spanish characters

Good morning,

I have a PHP website in Spanish. Unfortunately some characters like ñ or á are replaced by a �.
I have read lots of papers regarding how to solve it without any success.
Here the code:

<div>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
    include ("head_spanish.html");
    include ("menu_spanish.html");
    include ("/home/libroman/php_library/database.php");
    header('Content-Type: text/html; charset=utf-8' );
    ini_set('default_charset', 'utf-8');
?>
    <div class="bordergrey_bottom text_red">
        <h2 class="margin_0 padding_3 text_140">
            <p>
                Here I enter some Spanish text
            </p>            
        </h2>
    </div>

Any help would be really appreciated.
Thanks a lot.

Where is the text coming from, is it a database or types into you html?

^ This part should be within the head section of the html, not the body.

Be sure to save all files (php scripts and html templates) in utf-8 encoding.

1 Like

Hello Sam,

Thanks for your answer. Text is typed in the same file, not from database:

            <p>
                Here I enter some Spanish text
            </p> 

I have created section as suggested leaving the code like this, but the issue was not solved and � is still showed.

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<div id="container" class="bordergrey_all">
<?php

    include ("head_spanish.html");
    include ("menu_spanish.html");
    include ("/home/libroman/php_library/database.php");
    header('Content-Type: text/html; charset=utf-8' );
    ini_set('default_charset', 'utf-8');
?>
    <div class="bordergrey_bottom text_red">
        <h2 class="margin_0 padding_3 text_140">
            <p>
                Here I enter some Spanish text
            </p>            
        </h2>
    </div>

It sounds like your text editor isn’t saving in UTF-8 but most likely default ASCII

You should also be placing all header() declarations at the top of your page.

I can already see headers already sent error in the midst.

Hello Mittineague,

Text editor should be working fine. For instance, I have made other pure HTML pages where all characters are showed well. The issue is only with PHP pages.
If I open the page wuth Mozzila Firefox, press F10 to activate menu and then View->Text encoding-> and select Western, all looks well, so I think the issue is with the character encoding.

Thanks again.

Hello Spaceshiptrooper,

Could you please be more specific? I am not sure what you are suggesting me to do.

Thanks.

You have headers here:-

after there has been output to the page. Headers must come before any output.

The html does not appear to be valid. There are no body tags and the character set is alone in the head.

2 Likes

You have done this first.

Before your header() declaration which means that you will most likely see headers already sent error if you ever turn on your error logs. Error logs are basically the best way to debug your code and figure out why it isn’t doing what you expect it to do. Sometimes, the errors might be obvious and point you in the obvious direction.

Here, I suggest placing the two lines that have to deal with the page configuration first at the top of your page before doing anything else.

Hi Sam74,

I have modified according to your ideas. Here it is now, the issue was not solved:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
    <div id="container" class="bordergrey_all">

<?php
        header('Content-Type: text/html; charset=utf-8' );
        ini_set('default_charset', 'utf-8');
        include ("head_spanish.html");
        include ("menu_spanish.html");
        include ("/home/libroman/php_library/database.php");
?>

        <div class="bordergrey_bottom text_red">
            <h2 class="margin_0 padding_3 text_140">
                <p>
                    Text in Spanish
                </p>         
            </h2>
        </div>

That is still incorrect. What was meant by “top of your page” was “at the very beginning of the file”.

Do you mean like this? (not solved):

<?php
    header('Content-Type: text/html; charset=UTF-8' );
    ini_set('default_charset', 'UTF-8');
?>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
    <div id="container" class="bordergrey_all">

<?php
        include ("head_spanish.html");
        include ("menu_spanish.html");
        include ("/home/libroman/php_library/database.php");
?>

        <div class="bordergrey_bottom text_red">
            <h2 class="margin_0 padding_3 text_140">
                <p>
                    Text in Spanish
                </p>            
            </h2>
        </div>

Guys,

Issue was solved using this last code order and replacing encoding UTF-8 by windows-1252.

Thanks a lot for your help!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.