php html_entity_decode 是否替换 还?如果不行怎么更换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6275380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does html_entity_decode replaces also? If not how to replace it?
提问by Abhishek Sanghvi
I have a situation where I am passing a string to a function. I want to convert to " " (a blank space) before passing it to function. Does html_entity_decodedoes it?
我有一种情况,我将字符串传递给函数。我想 在将它传递给函数之前转换为“”(一个空格)。行html_entity_decode吗?
If not how to do it?
如果不行怎么办?
I am aware of str_replacebut is there any other way out?
我知道,str_replace但还有其他出路吗?
回答by Salman A
Quote from html_entity_decode()manual:
引用自html_entity_decode()手册:
You might wonder why
trim(html_entity_decode(' '));doesn't reduce the string to an empty string, that's because the' 'entity is not ASCII code 32 (which is stripped by trim()) but ASCII code 160 (0xa0) in the default ISO 8859-1 characterset.
您可能想知道为什么
trim(html_entity_decode(' '));不将字符串缩减为空字符串,那是因为' '实体不是 ASCII 代码 32(由 trim() 剥离),而是默认 ISO 8859-1 字符集中的 ASCII 代码 160 (0xa0)。
You can use str_replace()to replace the ascii character #160 to a space:
您可以使用str_replace()将 ascii 字符 #160 替换为空格:
<?php
$a = html_entity_decode('> <');
echo 'before ' . $a . PHP_EOL;
$a = str_replace("\xA0", ' ', $a);
echo ' after ' . $a . PHP_EOL;
回答by Frederic Bazin
YES
是的
See PHP manual http://php.net/manual/en/function.html-entity-decode.php.
请参阅 PHP 手册http://php.net/manual/en/function.html-entity-decode.php。
Carefully read the Notes, maybe that s the issue you are facing:
仔细阅读注释,也许这就是你面临的问题:
You might wonder why trim(html_entity_decode(' ')); doesn't reduce the string to an empty string, that's because the ' ' entity is not ASCII code 32 (which is stripped by trim()) but ASCII code 160 (0xa0) in the default ISO 8859-1 characterset.
你可能想知道为什么 trim(html_entity_decode(' ')); 不会将字符串缩减为空字符串,这是因为在默认 ISO 8859-1 字符集中,' ' 实体不是 ASCII 代码 32(由 trim() 剥离)而是 ASCII 代码 160 (0xa0)。
回答by Aurimas
html_entity_decode does convert to a space, just not a "simple" one (ASCII 32), but a non-breaking space (ASCII 160) (as this is the definition of ).
html_entity_decode 确实转换 为空格,只是不是“简单”空格(ASCII 32),而是不间断空格(ASCII 160)(因为这是 的定义 )。
If you need to convert to ASCII 32, you still need a str_replace(), or, depending on your situation, a preg_match("/s+", ' ', $string)to convert all kinds of whitespace to simple spaces.
如果您需要转换为 ASCII 32,您仍然需要 a str_replace(),或者,根据您的情况, apreg_match("/s+", ' ', $string)将各种空格转换为简单空格。
回答by Tyler Christian
Not sure if it is a viable solution for most cases but I used trim(strip_tags(html_entity_decode(htmlspecialchars_decode($html), ENT_QUOTES, 'UTF-8')));in my most recent application. The addition of htmlspecialchars_decode()initially was the only thing that would actually strip them.
不确定在大多数情况下它是否是可行的解决方案,但我trim(strip_tags(html_entity_decode(htmlspecialchars_decode($html), ENT_QUOTES, 'UTF-8')));在最近的应用程序中使用过。htmlspecialchars_decode()最初的添加是唯一能真正剥离它们的东西。

