Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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转换html&;nbsp;空间,;燃气轮机;至>;等_Php_Html - Fatal编程技术网

PHP转换html&;nbsp;空间,;燃气轮机;至>;等

PHP转换html&;nbsp;空间,;燃气轮机;至>;等,php,html,Php,Html,我想将所有html标记( ><等)转换为文本格式; 我试过了 html_entity_decode() 但它会回来吗?如果与之相反,则它会将字符串中的所有HTML实体转换为其适用的字符 $orig = "I'll \"walk\" the <b>dog</b> now"; $a = htmlentities($orig); $b = html_entity_decode($a); echo $a; // I'll &quot;walk&a

我想将所有html标记( ><等)转换为文本格式; 我试过了

html_entity_decode() 
但它会回来吗?如果与之相反,则它会将字符串中的所有HTML实体转换为其适用的字符

$orig = "I'll \"walk\" the <b>dog</b> now";

$a = htmlentities($orig);

$b = html_entity_decode($a);

echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now

echo $b; // I'll "walk" the <b>dog</b> now
$orig=“我现在就去遛狗”;
$a=htmlentities($orig);
$b=html_实体_解码($a);
回声$a;//我现在“走”b日志/b
回声$b;//我现在就去遛狗

使用html\u entity\u decode()代替html\u entity\u encode()

使用
htmlspecialchars\u decode
htmlspecialchars
相反。
PHP文档页面中的示例:

    $str = '<p>this -&gt; &quot;</p>';
    echo htmlspecialchars_decode($str); 
    //Output: <p>this -> "</p>
$str='this-“

”; echo htmlspecialchars_解码($str); //输出:此->“

如果您查看手册:

你可能想知道为什么trim(html_entity_decode(“”));不 将字符串减少为空字符串,这是因为“” 实体不是ASCII代码32(由trim()剥离),而是ASCII代码 默认ISO 8859-1字符集中的代码160(0xa0)

您可以将html_entity_decode()函数嵌套在to ASCII#160中的空格中:

<?php

echo str_replace("\xA0", ' ', html_entity_decode('ABC &nbsp; XYZ') );

?>

我知道我的答案来得很晚,但我想它可能会帮助其他人。我发现提取所有特殊字符的最佳方法是在php中使用。即使是处理
或任何其他表示空白的特殊字符,也要使用
utf8\u decode()

使用
utf8\u decode()
后,可以直接在代码中操作这些字符。例如,在下面的代码中,函数clean()将
替换为空白。然后使用
preg\u replace()
将所有额外的空格替换为一个空格。使用
trim()
删除前导空格和尾随空格

你好,世界!同侧眼线


你为什么不测试一下?也可以在手册页上向下滚动,因为其中列出了相关功能。@rikiless,RavatSinh的解决方案为我删除了所有HTML标记,包括。
    $str = '<p>this -&gt; &quot;</p>';
    echo htmlspecialchars_decode($str); 
    //Output: <p>this -> "</p>
<?php

echo str_replace("\xA0", ' ', html_entity_decode('ABC &nbsp; XYZ') );

?>
function clean($str)
{       
    $str = utf8_decode($str);
    $str = str_replace("&nbsp;", "", $str);
    $str = preg_replace("/\s+/", " ", $str);
    $str = trim($str);
    return $str;
}

$html = "&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;Hello world! lorem ipsum.";
$output = clean($html);
echo $output;