如何正确地将输入转换为 HTML 字符
假设我包含一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的代码只是通过 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.
您应该在正在查看此内容的 HTML 页面上将编码设置为 UTF-8。
htmlentities
根本不影响此文本。我用下面的代码尝试了同样的东西,效果很好:
index.php
test.php
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
test.php
输出指定字符编码的 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.
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
这就是最终在你和我的两个不同的代码上工作的方法;原因很难知道,但与解析有关。
这是浏览器显示的(FF + IE)-->
替代文本 http://i77.photobucket。 com/albums/j65/speedcoder/4-3-20101-22-31PM.png
示例**(不使用“include”功能,因此不需要输出缓冲区):
这个对我不起作用:
如果上面的示例使用包含 html 代码的包含文件,那么至少对我而言,它没有转换字符。我将其更改为不包含文件并使用 utf8_encode,但问题是我的代码需要使用不起作用的包含函数。
下面的下一个示例使用 include 方法和输出缓冲区,允许在 utf8_encode 编码发生之前渲染和解析代码。
我的代码场景(对于我的特定场景,必须使用 ob,因为包含文件还包含需要首先解析的代码):
感谢您帮助我弄清楚“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):
This one didn't work for me:
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):
Thanks for helping me figure it out "Ondrej Slinták"!!!