html_entity_decode会替换 也?如果不是如何更换它?

时间:2022-11-16 13:04:05

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_decode does it?

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

If not how to do it?

如果没有怎么办?

I am aware of str_replace but is there any other way out?

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

3 个解决方案

#1


32  

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.

您可能想知道为什么修剪(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('>&nbsp;<');
echo 'before ' . $a . PHP_EOL;
$a = str_replace("\xA0", ' ', $a);
echo ' after ' . $a . PHP_EOL;

#2


5  

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('&nbsp;')); 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.

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

#3


3  

html_entity_decode does convert &nbsp; to a space, just not a "simple" one (ASCII 32), but a non-breaking space (ASCII 160) (as this is the definition of &nbsp;).

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,你仍然需要一个str_replace(),或者,根据你的情况,preg_match(“/ s +”,'',$ string)将所有类型的空格转换为简单空格。

#1


32  

Quote from html_entity_decode() manual:

引自html_entity_decode()手册:

You might wonder why trim(html_entity_decode('&nbsp;')); doesn't reduce the string to an empty string, that's because the '&nbsp;' entity is not ASCII code 32 (which is stripped by trim()) but ASCII code 160 (0xa0) in the default ISO 8859-1 characterset.

您可能想知道为什么修剪(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('>&nbsp;<');
echo 'before ' . $a . PHP_EOL;
$a = str_replace("\xA0", ' ', $a);
echo ' after ' . $a . PHP_EOL;

#2


5  

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('&nbsp;')); 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.

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

#3


3  

html_entity_decode does convert &nbsp; to a space, just not a "simple" one (ASCII 32), but a non-breaking space (ASCII 160) (as this is the definition of &nbsp;).

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,你仍然需要一个str_replace(),或者,根据你的情况,preg_match(“/ s +”,'',$ string)将所有类型的空格转换为简单空格。