【问题标题】:Parsing a string which I am not able to identify as Object or Array or Json解析我无法识别为 Object 或 Array 或 Json 的字符串
【发布时间】:2012-06-04 18:37:36
【问题描述】:

我收到一个响应请求的数组。当我对它执行 print_r 时,这就是我得到的 Array( [receiver] => {:email=>"email@domain.com"})

我无法理解如何访问 ":email" 的值。

请帮忙。

编辑:

这是响应的 var_dump。

array ( 'receiver' => '{:email=>"email@domain.com"}' )

谢谢。

【问题讨论】:

  • 你能发布var_dump($yourarray)的输出吗?
  • 参见 @Pateman 的 cmets 关于 Mike B 的回答。你有的是字符串{:email=>"email@domain.com"},所以你必须自己解析它。
  • 我删除了我的答案,因为我对您的数据结构的假设不以为然。祝你好运!
  • @bfavaretto 抱歉回复晚了。我已经用响应的 var_dump 编辑了问题。除了使用正则表达式之外还有其他方法吗?
  • 嗯,您可以使用trim()explode() 构建此类字符串的基本解析器。

标签: php arrays parsing object


【解决方案1】:

使用正则表达式获取邮件。

$arr = array('recebodor' => '{:email=>"someone@example.com"}');

$email = preg_replace('/{:email=>"([^"]+)"}/', '$1', $arr['recebodor']);
echo $email; // someone@example.com

说明:

{:email=>    Match with the "{:email=>" in the string
"([^"]+)"    Get any character within double quotes
}            Match with the last "}"
$1           Replace all text with the text found inside parentheses

【讨论】:

  • 好的。谢谢。但是“:电子邮件”不应该是某些数据结构的一部分吗?我们应该可以通过遍历或其他方式访问它?
  • @bsdnoobz 我在您的回答中编辑了电子邮件地址。这种事情在 StackOverflow 上被视为垃圾邮件。
  • @bfavaretto 我不知道。谢谢!
猜你喜欢
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 2015-04-19
  • 1970-01-01
  • 2013-05-12
相关资源
最近更新 更多