开发者

How can I convert input to HTML Characters correctly

开发者_开发百科

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�


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


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">
<html xmlns="http://www.w3.org/1999/xhtml">
    <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>


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


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


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 http://i77.photobucket.com/albums/j65/speedcoder/4-3-20101-22-31PM.png

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"!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜