删除"\ ufeff"从对象的末尾开始-> Google+ API json结果中的内容 [英] Removing the "\ufeff" from the end of object -> content in Google+ API json result

查看:91
本文介绍了删除"\ ufeff"从对象的末尾开始-> Google+ API json结果中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google+ API的结果被\ufeff附加到每个内容"结果的末尾(我真的不知道为什么吗?)

The result from the Google+ API has \ufeff appended to the end of every "content" result (I don't really know why?)

从json结果中删除此unicode字符的最佳方法是什么?在我显示的某些输出中,它会产生一个'?'.

What is the best way to remove this unicode character from the json result? It is producing a '?' in some of the output I am displaying.

示例:

https://developers.google.com/+/api/latest/activities/get#try-it 

输入活动ID

z12pvrsoaxqlw5imi22sdd35jwvkglj5204

并单击执行,结果将是:

and click Execute, result will be:

{
 .....
 "object": {
  ......
  "content": "CONTENT OF GOOGLE PLUS POST HERE \ufeff",
  ......

示例PHP代码显示?" "\ ufeff"所在的位置:

example PHP code which shows a '?' where the '\ufeff' is:

<?php
$data = json_decode($result_from_google_plus_api, true);
echo $data['object']['content'];
// outputs "CONTENT OF GOOGLE PLUS POST HERE ?"
echo trim($data['object']['content']);
// outputs "CONTENT OF GOOGLE PLUS POST HERE ?"

还是我会以错误的方式进行操作?我应该修正?"问题,而不是尝试删除'\ufeff'?

Or am I going about this the wrong way? Should I be fixing the '?' issue rather than trying to remove the '\ufeff'?

推荐答案

在您的情况下,您可以使用此正则表达式:

In your case, you could use this regexp:

$str = preg_replace('/\x{feff}$/u', '', $str);

这样,您就可以完全匹配该代码点值并将其删除.

That way you can exactly match that code point value and have it removed.

根据我的经验,您想删除更多空白字符.从 my 经历过,这对 me 效果很好:

From my experience there are a lot more white-spacey-character you want to remove. From my experienced this works well for me:

# I like to call this unicodeTrim()
$str = preg_replace(
  '/
    ^
    [\pZ\p{Cc}\x{feff}]+
    |
    [\pZ\p{Cc}\x{feff}]+$
   /ux',
  '',
  $str
);

我发现 http://www.regular-expressions.info/unicode.html有关详细细节的很好的资源:

I found http://www.regular-expressions.info/unicode.html a pretty good resource about the fine details:

  • \pZ-匹配任何类型的空格或不可见的分隔符
  • \p{Cc}-匹配控制字符
  • \x{feff}-匹配BOM
  • \pZ - match any kind of whitespace or invisible separator
  • \p{Cc} - match control characters
  • \x{feff} - match BOM

我已经看到正则表达式建议匹配\pC而不是\pCc,但这很危险,因为pC包括没有分配字符的任何代码点.因此,我已经删除了实际数据(某些表情符号或其他内容).

I've seen regex suggest to match \pC instead of \pCc, however this is dangerous because pC includes any code point to which no character has been assigned. I've had actual data (certain emojis or other stuff) being removed because of this.

但是,YMMW,我不能强调这一点.

But, YMMW, I cant' stress this.

这篇关于删除"\ ufeff"从对象的末尾开始-> Google+ API json结果中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆