html_entity_decode 是否替换  还?如果不是怎么更换?

发布于 2024-11-14 19:46:10 字数 175 浏览 6 评论 0原文

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

如果不是怎么办?

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

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?

If not how to do it?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不…忘初心 2024-11-21 19:46:10

引用自 html_entity_decode() 手册:

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

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

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

Quote from html_entity_decode() manual:

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.

You can use str_replace() to replace the ascii character #160 to a space:

<?php
$a = html_entity_decode('> <');
echo 'before ' . $a . PHP_EOL;
$a = str_replace("\xA0", ' ', $a);
echo ' after ' . $a . PHP_EOL;
小耗子 2024-11-21 19:46:10

html_entity_decode 确实将   转换为空格,只是不是一个“简单”的空格(ASCII 32),而是一个不间断的空格(ASCII 160)(因为这是 的定义 )。

如果您需要转换为 ASCII 32,您仍然需要 str_replace(),或者根据您的情况,需要 preg_match("/s+", ' ', $string) code> 将各种空白转换为简单空格。

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  ).

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.

不寐倦长更 2024-11-21 19:46:10

请参阅 PHP 手册 http://php.net/manual/en/ function.html-entity-decode.php

仔细阅读注释,也许这就是您面临的问题:

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

YES

See PHP manual 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.

无风消散 2024-11-21 19:46:10

不确定这对于大多数情况是否是可行的解决方案,但我在最近的应用程序中使用了 trim(strip_tags(html_entity_decode(htmlspecialchars_decode($html), ENT_QUOTES, 'UTF-8')));。最初添加 htmlspecialchars_decode() 是唯一真正能剥离它们的东西。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文