PHP htmlentities即使使用参数也不起作用 [英] PHP htmlentities not working even with parameters
问题描述
当然,这已经被问过了,并已经寻找解决方案,但到目前为止,所有解决方案都还没有奏效.我想通过使用htmlentities
或htmlspecialchars
来将TM符号和&符号更改为它们的html等价物:
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');
这将显示:
ORGINIAL: Kold Locker™ & other stuff
HTML:
我还尝试过使用htmlspecialchars
进行尝试,并且更改了第二个参数,结果相同.
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?
更新:我尝试仅显示utf8_encode($TEST)
,并且显示HTML: Kold Locker™ & other stuff
UPDATE: I tried just displaying utf8_encode($TEST)
and it displayed HTML: Kold Locker™ & other stuff
推荐答案
您的代码对我有用:-?
Your code works for me :-?
在htmlentities()
的手册页中,我们可以阅读:
In the manual page for htmlentities()
we can read:
返回值
返回编码的字符串.
如果输入字符串中包含无效的代码单元序列 给定的编码将返回一个空字符串,除非 设置了ENT_IGNORE或ENT_SUBSTITUTE标志.
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.
我的猜测是输入数据未正确编码为UTF-8,并且该函数返回空字符串. (假设脚本没有崩溃,即该部分之后的代码仍在运行.)
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.)
这篇关于PHP htmlentities即使使用参数也不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!