我有一个带有“\\\ ”的字符串,我需要将其替换为“” str_replace失败 [英] I have a string with "\u00a0", and I need to replace it with "" str_replace fails
问题描述
我需要清理各种Microsoft Office套件应用程序(复制/粘贴)的字符串( Excel ,访问和 Word ),每个都有自己的编码集。
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.
我正在使用json_encode进行调试为了能够看到每一个编码的字符。
I'm using json_encode for debugging purposes in order to being able to see every single encoded character.
我可以用str_replace清理我发现的所有东西(\r \\\
),但是\\\ 我没有运气。
I'm able to clean everything I found so far (\r \n) with str_replace, but with \u00a0 I have no luck.
$string = 'mail@mail.com\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0;mail@mail.com'; //this is the output from json_encode
$clean = str_replace("\u00a0", "",$string);
返回:
mail@mail.com\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0;mail@mail.com
完全一样;它完全忽略了\\\ 。
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____
在前两次回复之后,我需要澄清一点,我的示例工作,因为它是json_encode的输出,而不是实际的字符串!
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!
推荐答案
当我复制/粘贴您的代码时,适用于我。尝试使用单引号替换 str_replace()
中的双引号,或者转义反斜杠(\\\\
)。
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"
).
这篇关于我有一个带有“\\\ ”的字符串,我需要将其替换为“” str_replace失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!