简体   繁体   English

PHP htmlentities 即使使用参数也不起作用

[英]PHP htmlentities not working even with parameters

Of course this has been asked before and have searched for solutions, all which have not worked thus far.当然,这之前已经被问过并已经寻找了解决方案,但迄今为止所有这些都没有奏效。 I want to change out the TM symbol and the ampersand to their html equivelents by using htmlentities or htmlspecialchars :我想通过使用htmlentitieshtmlspecialchars将 TM 符号和 & 符号更改为它们的 html 等效项:

$TEST = "Kold Locker™ & other stuff";
echo "ORGINIAL: " . $TEST . "<BR/>";

echo "HTML: " . htmlentities($TEST, ENT_COMPAT, 'UTF-8');

This displays:这显示:

ORGINIAL: Kold Locker™ & other stuff
HTML: 

I have also tried it with htmlspecialchars and the second parameter changed with the same result.我也用htmlspecialchars尝试过它,第二个参数改变了相同的结果。

What am I missing that others have claimed worked in other solutions?我错过了其他人声称在其他解决方案中有效的东西?

UPDATE : I tried just displaying utf8_encode($TEST) and it displayed HTML: Kold Locker™ & other stuff更新:我尝试只显示utf8_encode($TEST)并显示HTML: Kold Locker™ & other stuff

I dont know why , this worked for me ( htmlentities has to be called twice for me )我不知道为什么,这对我有用必须为我调用 htmlentities 两次

$html="<html> <head><head>something like this   </html>"
$entities_correction= htmlentities( $html, ENT_COMPAT, 'UTF-8');
echo  htmlentities( $entities_correction, ENT_COMPAT, 'UTF-8');

output :输出 :

&lt;html&gt; &lt;head&gt;&lt;head&gt;something like this &lt;/html&gt;

I thought I had the same problem as Pjack (msg of Jul 14 at 8:54):我以为我和 Pjack 有同样的问题(7 月 14 日 8:54 的 msg):

$str = "A 'quote' is <b>bold</b>";
echo htmlentities($str); 

gives in the Browser (Firefox in my case) the original string $str (without any translation), while在浏览器(在我的情况下为 Firefox)中给出原始字符串 $str (没有任何翻译),而

echo htmlentities(htmlentities($str));

gives:给出:

A 'quote' is &lt;b&gt;bold&lt;/b&gt; 

(I use PHP/5.4.16 obtained from windows-7 XAMPP). (我使用从 windows-7 XAMPP 获得的 PHP/5.4.16)。

However, after some more thought it occurred to me that the Browser shows the strings &lt;然而,经过一些思考,我发现浏览器显示字符串 &lt; and &gt;和 &gt; as > and <.作为 > 和 <。 (See the source code in the browser). (查看浏览器中的源代码)。 Second call of htmlentities translates & into &amp; htmlentities 的第二次调用将 & 转换为&amp; and only then the Browser shows what you expected in the first place.只有这样,浏览器才会首先显示您的预期。

Your code works for me :-?你的代码对我有用:-?

In the manual page for htmlentities() we can read:htmlentities()手册页中,我们可以阅读:

Return Values返回值

Returns the encoded string.返回编码的字符串。

If the input string contains an invalid code unit sequence within the given encoding an empty string will be returned, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set.如果输入字符串在给定的编码中包含无效的代码单元序列,则将返回一个空字符串,除非设置了 ENT_IGNORE 或 ENT_SUBSTITUTE 标志。

My guess is that the input data is not properly encoded as UTF-8 and the function is returning an empty string.我的猜测是输入数据没有正确编码为 UTF-8,函数返回一个空字符串。 (Assuming that the script is not crashing, ie, code after that part still runs.) (假设脚本没有崩溃,即该部分之后的代码仍在运行。)

I had almost the same problem (in which somehow it showed the same text every time) and with a combination of different echo´si got it.我遇到了几乎相同的问题(不知何故,它每次都显示相同的文本)并且结合了不同的 echo´si 得到了它。 It seems that webbrowsers like firefox show the same text every time.似乎像 Firefox 这样的网络浏览器每次都显示相同的文本。 That´s because when you echo the htmlentities-text, its being converted back into normal text while echoing.那是因为当您echo显 htmlentities-text 时,它会在回显时转换回普通文本。 When I echo a script with the variable/text to be console.log ged, it actually echo´s the htmlentities text (almost) correctly.当我将带有变量/文本的脚本回显为console.log ,它实际上(几乎)正确地回显了 htmlentities 文本。 Instead of replacing every special char with html-codings, it replaces ´em with some other coding i already saw before (I can´t remember the name).它没有用 html 编码替换每个特殊字符,而是用我以前见过的其他一些编码替换 'em(我不记得名字了)。 Htmlentities-ing it again, I get the same text echo´d again (remember it converts everything), but echoing it in console.log-version gives to me the expected result. Htmlentities-ing 再次,我再次得到相同的文本 echo´d(记住它会转换所有内容),但是在 console.log-version 中回显它给了我预期的结果。 Now, again, as a result:现在,再次,结果:
1. Execute htmlentities two times ! 1. 执行htmlentities两次
2. Don´t (at least with firefox) echo the htmlentities as normal into the webpage . 2.不要(至少在 Firefox 中)像往常一样将 htmlentities到网页中 If you´d like to check if the value is actually correct, echo a script that logs it into console.如果您想检查该值是否确实正确,请回显将其记录到控制台的脚本。
I hope this could help other guys with the same problem,我希望这可以帮助其他有同样问题的人,
VicStudio维克工作室

EDIT : 3. If you are using a $_POST formular, don´t forget to add accept-charset="UTF-8" (or some other charset) to the <form> tag.编辑: 3. 如果您使用的是$_POST公式,请不要忘记将accept-charset="UTF-8" (或其他一些字符集)添加到<form>标记中。

EVEN MORE EDIT : Only do 2 times htmlentities if you wish to echo your result normal into the page.甚至更多编辑:如果您希望将结果正常回显到页面中, htmlentities执行 2 次htmlentities If you wish to directly send in fe a database, only do it once!如果您想直接发送 fe 数据库,只需做一次! -> what i said before is partually wrong. -> 我之前说的部分是错误的。 :( :(

这是一篇旧帖子,但对于仍在寻找解决方案的任何人,以下是我成功使用的方法:

echo html_entity_decode($htmlString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM