我有一个带有“\u00a0”的字符串,我需要将其替换为“” str_replace 失败
我需要清理来自(复制/粘贴)来自各种 Microsoft Office 套件应用程序的字符串(Excel 、访问和Word),每个都有自己的一套编码。
我使用 json_encode 进行调试,以便能够看到每个编码字符。
我可以使用 str_replace 清理迄今为止发现的所有内容 (\r \n),但使用 \u00a0 则运气不佳。
$string = '[email protected]\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0;[email protected]'; //this is the output from json_encode
$clean = str_replace("\u00a0", "",$string);
返回:
[email protected]\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0;[email protected]
完全一样;它完全忽略\u00a0。
有办法解决这个问题吗?另外,我觉得我正在重新发明轮子,是否有一个函数/类可以完全剥离所有可能的编码的每个可能的字符?
____编辑____
在前两个回复之后,我需要澄清我的示例确实有效,因为它是 json_encode 的输出,而不是实际的字符串!
I need to clean a string that comes (copy/pasted) from various Microsoft Office suite applications (Excel, Access, and Word), each with its own set of encoding.
I'm using json_encode for debugging purposes in order to being able to see every single encoded character.
I'm able to clean everything I found so far (\r \n) with str_replace, but with \u00a0 I have no luck.
$string = '[email protected]\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0;[email protected]'; //this is the output from json_encode
$clean = str_replace("\u00a0", "",$string);
returns:
[email protected]\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0;[email protected]
That is exactly the same; it completely ignores \u00a0.
Is there a way around this? Also, I'm feeling I'm reinventing the wheel, is there a function/class that completely strips EVERY possibile char of EVERY possible encoding?
____EDIT____
After the first two replies I need to clarify that my example DOES work, because it's the output from json_encode, not the actual string!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
通过在包含 \u00a0 的字符串上将
ord()
与substr()
结合使用,我发现以下诅咒有效:By combining
ord()
withsubstr()
on my string containing \u00a0, I found the following curse to work:试试这个:
Try this:
当我复制/粘贴您的代码时,对我有用。尝试将
str_replace()
中的双引号替换为单引号,或转义反斜杠 ("\\u00a0"
)。Works for me, when I copy/paste your code. Try replacing the double quotes in your
str_replace()
with single quotes, or escaping the backslash ("\\u00a0"
).我刚刚遇到了同样的问题。显然,PHP 的 json_encode 将为任何包含“不间断空格”的字符串返回 null。
解决方案是将其替换为常规空间:
我希望这对某人有帮助 - 我花了一个小时才弄清楚。
I just had the same problem. Apparently PHP's json_encode will return null for any string with a 'non-breaking space' in it.
The Solution is to replace this with a regular space:
I hope this helps somebody - it took me an hour to figure out.
这个也有效,我在某处找到的
This one also works, i found somewhere
一个小点: \u00a0 实际上是一个不间断的空格字符,参见 http ://www.fileformat.info/info/unicode/char/a0/index.htm
因此将其替换为“”可能更正确
A minor point: \u00a0 is actually a non-breaking space character, c.f. http://www.fileformat.info/info/unicode/char/a0/index.htm
So it might be more correct to replace it with " "
这对我来说很有效:
This did the trick for me:
您可以使用 json_encode($string, JSON_UNESCAPED_UNICODE |JSON_PRETTY_PRINT);
You can use
json_encode($string, JSON_UNESCAPED_UNICODE |JSON_PRETTY_PRINT);
您必须使用单引号来执行此操作,如下所示:
或者,如果您喜欢使用双引号,则必须转义反斜杠 - 如下所示:
You have to do this with single quotes like this:
Or, if you like to use double quotes, you have to escape the backslash - which would look like this: