Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php htmlentities函数中某些行为异常的字符_Php_Html_Html Entities - Fatal编程技术网

Php htmlentities函数中某些行为异常的字符

Php htmlentities函数中某些行为异常的字符,php,html,html-entities,Php,Html,Html Entities,所以首先我注意到,当一个符号(&)出现在htmlentities函数中时,它会将该符号计为5个字符。所以这个代码: $a = htmlentities("&12345"); $b = substr($a,0,6); echo $b; 将回声'&1'作为,我相信它正在计数&作为5个字符。 更有趣的是,GBP符号(£)一起被忽略,因此: $a = htmlentities("£"); echo $a; 什么都没有。我在Chrome和FF上得到了相同的结果。我不知道这是一个

所以首先我注意到,当一个符号(&)出现在htmlentities函数中时,它会将该符号计为5个字符。所以这个代码:

$a = htmlentities("&12345");
$b = substr($a,0,6);
  echo $b; 
将回声
'&1'
作为,我相信它正在计数&作为5个字符。 更有趣的是,GBP符号(£)一起被忽略,因此:

$a = htmlentities("£");
  echo $a;   
什么都没有。我在Chrome和FF上得到了相同的结果。我不知道这是一个bug还是我应该使用不同的语法。有人知道为什么会这样吗?谢谢

更新 我已经解决了这个问题:
a=htmlentities(“£”,entu COMPAT,'ISO-8859-15')

但符号问题仍然存在。

问题在于,您正在浏览器窗口中查看输出,其中来自
htmlentities
(以及等效函数)的输出将呈现给最终用户显示

例如:

echo htmlentities("&");
将输出
&&
字符转换为它的html实体等价物。请注意,它有五个字符长。但是,您看不到全文
&因为您正在从浏览器中查看,该浏览器已将其预呈现为
&
符号。在firfox中,如果右键单击视口并单击“查看页面源代码”,您将看到全文
&

您的代码:

$a = htmlentities("&12345"); //Outputs: &12345
$b = substr($a, 0, 6); //Selects first six charachters: &, a, m, p, ; and 1
echo $b; // Echo's: &1 which is displayed by the browser as &1
要解决此问题,您可以更改函数的顺序:

$a = substr("&12345", 0, 6);
echo htmlentities($a);

echo-htmlentities(£)在我的末尾给出
英镑
。使用Chrome中的“查看源”检查原始源。请尝试mb_substr而不是substr。您很可能看到的是浏览器输出,而不是html源代码输出,但没有任何更改,谢谢您在这里可以发挥作用,只需在整个系统中使用UTF-8编码,所有这些问题都将消失。不需要实体,除了关键的HTML实体(
&
)。非常感谢您的解释。你知道我如何解决这个问题吗?这样它就会输出:
&12345
?是的,有一个简单的解决方案,只需在
htmlentities
之前使用
substr
。即
htmlentities(substr(“&12345”,0,6))--请参阅答案中的更新