【问题标题】:php - how to get specific value from the stringphp - 如何从字符串中获取特定值
【发布时间】:2017-03-05 14:19:05
【问题描述】:

我在尝试从字符串中提取特定值但没有得到正确输出时遇到问题。

$string = com.xyz.ghop.service.sprint.Sprint@1b0258c[id=257,rapidViewId=94,state=CLOSED,name=Alert Success 5,goal=,startDate=2016-08-16T20:20:46.730+05:30,endDate=2016-08-26T20:20:00.000+05:30,completeDate=2016-08-26T21:18:53.928+05:30,sequence=257]

我想要 name= 中的值直到逗号,我尝试使用 preg_match_all 但它只给出第一个单词。

这是我尝试过的:

preg_match_all("/(?=name\=([^\W]+))/", $string, $matches); 
$resulte = implode(",", $matches[1]); 

请帮忙

谢谢

【问题讨论】:

  • 你能展示一下你对preg_match的尝试吗?
  • preg_match_all("/(?=name\=([^\W]+))/", $string, $matches); $resulte = implode(",", $matches[1]);

标签: php string preg-match-all


【解决方案1】:

有多种方法,可用于 preg_match 或 preg_match_all

$results = preg_match('#name=(.*?),#', $matches);

$results = preg_match('#name=([^,]+),#', $matches);

【讨论】:

  • 谢谢。它只返回值而不是“name=Alert Success 5”只是“Alert Success 5”
  • @Areik 访问 $matches[1]
  • 正如@sidyll 所说,顺便说一句,我已经更新了答案
  • @HassanAhmed 现在你做了他所问的相反:D
  • :D 但它符合原始问题
【解决方案2】:

你可以试试这样的:

preg_match("/name=(.+?),/", $string, $matches);
$result = end($matches);

希望这会有所帮助!

【讨论】:

  • 为什么要使用 end ! $matches[1];只要你匹配一件事就足够了。
  • @HassanAhmed 为什么不... :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多