PHP 转换 html 到空间,> 到 > 等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15289772/
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
PHP convert html to space, > to > etc
提问by RavatSinh Sisodiya
I want to convert all html tags(  > < etc) to text format; I have try
我想将所有 html 标签( > < 等)转换为文本格式;我试过了
html_entity_decode()
but it will return ? if  .
但它会回来吗?如果 。
回答by RavatSinh Sisodiya
Use htmlspecialchars_decodeis the opposite of htmlspecialchars.
Example from the PHP documentation page:
使用htmlspecialchars_decode与 相反htmlspecialchars。
PHP 文档页面中的示例:
$str = '<p>this -> "</p>';
echo htmlspecialchars_decode($str);
//Output: <p>this -> "</p>
回答by Gung Foo
html_entity_decode()is the opposite of htmlentities()in that it converts all HTML entities in the string to their applicable characters.
html_entity_decode()与htmlentities()相反,它将字符串中的所有 HTML 实体转换为其适用的字符。
$orig = "I'll \"walk\" the <b>dog</b> now";
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll "walk" the <b>dog</b> now
echo $b; // I'll "walk" the <b>dog</b> now
回答by Tsimtsum
Use
用
html_entity_decode()代替
html_entity_encode()
回答by aka
If you check the 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(' ')); 不会将字符串缩减为空字符串,这是因为在默认 ISO 8859-1 字符集中,' ' 实体不是 ASCII 代码 32(由 trim() 剥离)而是 ASCII 代码 160 (0xa0)。
You can nest your html_entity_decode() function inside a str_replace()to ASCII #160 to a space:
您可以将 html_entity_decode() 函数嵌套在str_replace()到 ASCII #160 中的空格中:
<?php
echo str_replace("\xA0", ' ', html_entity_decode('ABC XYZ') );
?>
回答by Adam Ranganathan
I know my answer is coming in really late but thought it might help someone else. I find that the best way to extract all special characters is to use utf8_decode()in php. Even for dealing with or any other special character representing blank space use utf8_decode().
我知道我的答案来得很晚,但认为它可能会帮助其他人。我发现提取所有特殊字符的最佳方法是在 php 中使用utf8_decode()。甚至用于处理 或任何其他表示空格的特殊字符使用utf8_decode()。
After using utf8_decode()one can manipulate these characters directly in the code. For example, in the following code, the function clean() replaces with a blank. Then it replaces all extra white spaces with a single white space using preg_replace(). Leading and trailing white spaces are removed using trim().
使用后utf8_decode()可以直接在代码中操作这些字符。例如,在以下代码中,函数 clean() 替换 为空白。然后它使用一个空格替换所有额外的空格preg_replace()。使用 删除前导和尾随空格trim()。
function clean($str)
{
$str = utf8_decode($str);
$str = str_replace(" ", "", $str);
$str = preg_replace("/\s+/", " ", $str);
$str = trim($str);
return $str;
}
$html = " Hello world! lorem ipsum.";
$output = clean($html);
echo $output;
Hello world! lorem ipsum.
你好,世界!逻辑推理。

