如何正确地将输入转换为 HTML 字符

发布于 2024-08-27 23:20:09 字数 419 浏览 5 评论 0原文

假设我包含一个包含 html 的文件。 html 中的字符为感叹号、西班牙语重音符号 (á、ó)。解析后的包含文本将被处理为符号而不是其正确值。这种情况发生在 FF 上,但不会发生在 IE (8) 上。

我尝试了以下功能:

htmlspecialchars、htmlentities、utf8_encode

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

示例file.php内容:

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

输出:

Canci�n, �Song Name�

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�

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

两仪 2024-09-03 23:20:09

您的代码只是通过 htmlentities() 运行字符串“cont/fie.php”,文件的内容不受此影响。

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

﹉夏雨初晴づ 2024-09-03 23:20:09

您应该在正在查看此内容的 HTML 页面上将编码设置为 UTF-8。 htmlentities 根本不影响此文本。

我用下面的代码尝试了同样的东西,效果很好:

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>

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>
卸妝后依然美 2024-09-03 23:20:09

输出指定字符编码的 HTTP Content-Type 标头您在字符集参数中使用(建议使用UTF-8)。

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

所有深爱都是秘密 2024-09-03 23:20:09

echo htmlentities(file_get_contents("cont/file.php")); 可能是您要问的。
但是,如前所述,您不能使用 htmlentities,而应使用 UTB-8 编码

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

梦醒时光 2024-09-03 23:20:09

这就是最终在你和我的两个不同的代码上工作的方法;原因很难知道,但与解析有关。

这是浏览器显示的(FF + IE)-->

替代文本 http://i77.photobucket。 com/albums/j65/speedcoder/4-3-20101-22-31PM.png

示例**(不使用“include”功能,因此不需要输出缓冲区):

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

这个对我不起作用:

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

如果上面的示例使用包含 html 代码的包含文件,那么至少对我而言,它没有转换字符。我将其更改为不包含文件并使用 utf8_encode,但问题是我的代码需要使用不起作用的包含函数。

下面的下一个示例使用 include 方法和输出缓冲区,允许在 utf8_encode 编码发生之前渲染和解析代码。

我的代码场景(对于我的特定场景,必须使用 ob,因为包含文件还包含需要首先解析的代码):

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

感谢您帮助我弄清楚“Ondrej Slinták”!!!

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文