php - html_entity_decode 也会替换吗?如果不行怎么换?

标签 php html string whitespace html-entities

我有一种情况,我将一个字符串传递给一个函数。我想在将   传递给函数之前将其转换为“”(空格)。 html_entity_decode 可以吗?

如果不行怎么办?

我知道 str_replace 但还有其他出路吗?

最佳答案

引自 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.

您可以使用 str_replace()将 ascii 字符 #160 替换为空格:

<?php
$a = html_entity_decode('>&nbsp;<');
echo 'before ' . $a . PHP_EOL;
$a = str_replace("\xA0", ' ', $a);
echo ' after ' . $a . PHP_EOL;

关于php - html_entity_decode 也会替换吗?如果不行怎么换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6275380/

相关文章:

html - 如何自动在 Visual Studio 上按字母顺序排列 HTML 属性?

HTML Canvas 清晰画线

jquery - 我的下拉菜单移动其下方的内容

vba - 为什么 If Not IsEmpty 不过滤掉空字符串?

c - 为什么 char* 会导致未定义的行为而 char[] 不会?

php - mySQL仅存储部分字符串

php - Mysql从数据库中选择

php - SELECT * FROM table WHERE MONTH = 下个月的今天

php - symfony2 XLIFF 中的换行符 <br>

swift - 将字符串中的重音字符替换为基本字符 - ą -> a , ć -> c