简体   繁体   English

PHP:如何摆脱像“\ ”这样的奇怪字符?

[英]PHP: How to get rid of strange characters like “\u00a0”?

I got a messy bunch of JSON data to import into my database (for further purposes).我有一堆乱七八糟的 JSON 数据要导入到我的数据库中(用于进一步目的)。 When i checked them out (opened in a Text Editor), they contain so many weird (gibberish) characters like:当我检出它们(在文本编辑器中打开)时,它们包含许多奇怪(胡言乱语)的字符,例如:

For example, data.json :例如, data.json

[{"title":"hello world!","html_body":"<p>Hello\u00a0 from the\u00a0  other side.\u00a0 <\/p>"}]

And then, obviously, below code simply WON'T work:然后,显然,下面的代码根本行不通:

$clean = str_replace("\u00a0", "", $string);

Despite whatever those character are for, how can i get rid of them anyway please?不管这些角色是干什么用的,我怎么能摆脱它们呢?

Thanks for everyone in the comment section, who (at least) helped me to know those are non-breaking characters .感谢评论部分的每个人,他们(至少)帮助我知道那些non-breaking characters I then googled and found a working solution by myself anyhow:然后我用谷歌搜索,无论如何我自己找到了一个可行的解决方案:

$clean_html_body = preg_replace('/\xc2\xa0/', '', $html_body);

Thanks again all.再次感谢大家。 :) :)

If you have individual strings that might have non-breaking spaces or line returns at the end of them, you can trim these when putting together your JSON data by using this:如果您的单个字符串末尾可能有不间断空格或换行符,则可以在将 JSON 数据放在一起时使用以下方法修剪这些字符串:

$dat = trim($dat," \\t\\n\\r\\0\\x0B\\xc2\\xa0"); $dat = trim($dat," \\t\\n\\r\\0\\x0B\\xc2\\xa0");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM