PHP htmlentities not working even with parameters

懵懂的女人 提交于 2019-12-10 13:08:50

问题


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:

$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.

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


回答1:


Your code works for me :-?

In the manual page for htmlentities() we can read:

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.

My guess is that the input data is not properly encoded as UTF-8 and the function is returning an empty string. (Assuming that the script is not crashing, i.e., code after that part still runs.)




回答2:


I dont know why , this worked for me (htmlentities has to be called twice for me)

$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;




回答3:


I thought I had the same problem as Pjack (msg of Jul 14 at 8:54):

$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

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).

However, after some more thought it occurred to me that the Browser shows the strings &lt; and &gt; as > and <. (See the source code in the browser). Second call of htmlentities translates & into &amp; and only then the Browser shows what you expected in the first place.




回答4:


I had almost the same problem (in which somehow it showed the same text every time) and with a combination of different echo´s i got it. It seems that webbrowsers like firefox show the same text every time. That´s because when you echo the htmlentities-text, its being converted back into normal text while echoing. When I echo a script with the variable/text to be console.logged, it actually echo´s the htmlentities text (almost) correctly. 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). 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. Now, again, as a result:
1. Execute htmlentities two times!
2. Don´t (at least with firefox) echo the htmlentities as normal into the webpage. 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.

EVEN MORE EDIT: Only do 2 times htmlentities if you wish to echo your result normal into the page. If you wish to directly send in f.e. a database, only do it once! -> what i said before is partually wrong. :(



来源:https://stackoverflow.com/questions/15815899/php-htmlentities-not-working-even-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!