views:

184

answers:

5

Let's say I'm including a file which contains html. The html have characters as exclamation symbols, Spanish accents (á, ó). The parsed included text gets processed as symbols instead of their correct value. This happens on FF but not on IE (8).

I have tried the following functions:

htmlspecialchars, htmlentities, utf8_encode

include htmlentities("cont/file.php");

Sample file.php contents:

<div>Canción, “Song Name”</div>

Output:

Canci�n, �Song Name�
A: 

Output an HTTP Content-Type header that specifies the character encoding you are using (UTF-8 is recommended) in the charset parameter.

David Dorward
+2  A: 

Your code does nothing but to run the string "cont/fie.php" through htmlentities(), the content of the file is not affected by that.

Techpriester
A: 

echo htmlentities(file_get_contents("cont/file.php")); is what you're probably asking.
But, as mentioned before, you must not use htmlentities but UTB-8 encoding

Col. Shrapnel
I just need to search the include file for special characters and convert to html entities before include.
Codex73
@Codex73 if you already using utf-8, and your text `indeed` in utf-8, you need no character to be converted to entity. just throw htmlentities away
Col. Shrapnel
+1  A: 

You should set your encoding to UTF-8 on HTML page you are viewing this content on. htmlentities isn't affecting this text at all.

I tried the same stuff with following code and it worked fine:

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>TODO supply a title</title>
    </head>
    <body>
        <p>
            TODO write content


            <?php
                include "test.php";
            ?>

            </p>
    </body>
</html>

test.php

<div>ääääääó</div>
Ondrej Slinták
I'm using this: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Codex73
Try to check your browser's encoding. Maybe it's set to something different for some reason. I've tried your code and it works okay. I'll edit my post with code I've tried.
Ondrej Slinták
Are you sure you don't rewrite the page encoding with browser encoding if it happens just in FF? Could you try also any other browser? Just to be sure it's something within FF and not PHP side fault.
Ondrej Slinták
Also, try to check `default_charset` in your php.ini
Ondrej Slinták
Thanks for all of your help!
Codex73
No problem! I'm glad you figured it out :D
Ondrej Slinták
A: 

This is what end up working on two different your code and mine doing the trick; the reason being hard to know but something with parsing.

This is browser showed (FF + IE)-->

alt text

Sample** ('include' function not use, so Output Buffer not needed):

<?php 
$varr = '<div>ääääääó</div>'; 
echo utf8_encode($varr); 
?>

This one didn't work for me:

<?php
   include "test.php";
?>

If the above sample using an include file with html code it didn't convert at least for me the characters. I changed it to not been include file and worked with the utf8_encode, but the problem is that my code needs where using include function which din't work.

The next sample below uses include method and output buffer which allowed code to be rendered and parsed before utf8_encode encoding transpired.

My Code Scenario (for my specific scenario has to be with ob since include file also contains code which needs to be parsed first):

ob_start(); 
include ("cont/file.php"); 
$content = ob_get_contents(); 
ob_end_clean(); 
echo utf8_encode($content); 

Thanks for helping me figure it out "Ondrej Slinták"!!!

Codex73