使用preg_match_all匹配双引号内的数据

时间:2014-05-25 12:22:35

标签: php json preg-match-all data-extraction

下面给出的数据格式如" name":" Value"," name2":" Value 2",& #34; name3":"值3"

$datadump = '"Waived":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info", "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';

我试图使用php

提取名称
    $pattern = '/"(.*?)":/si';
preg_match_all($pattern, $datadump, $output);

但它没有用。 希望,知道解决它的人会帮助我。 谢谢。

6 个答案:

答案 0 :(得分:0)

这样可以正常工作:

$datadump = '"Waived":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info",      "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';
$pattern = '/\"([a-zA-Z\s]*?)\":/si';
preg_match_all($pattern, $datadump, $output);
$names = $output[1];
echo $names[2]; /* this is a simple test */

EDIT(仅限字母数字名称)

$datadump = '"a0":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info",      "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';
$pattern = '/\"([a-zA-Z0-9]*?)\":/si';
preg_match_all($pattern, $datadump, $output);
$names = $output[1];
for($i=0; $i<sizeof($names); $i++)
   echo $names[$i] . "\n";

EDIT2(它允许名称和冒号之间的空格)

$datadump = '"a0" : "Waived", "Until":" until", "HeaderBanInfo":"Ban Info",      "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';
$pattern = '/\"([a-zA-Z0-9]*?)\"[\s]*:/';
preg_match_all($pattern, $datadump, $output);
$names = $output[1];
for($i=0; $i<sizeof($names); $i++)
   echo $names[$i] . "\n";

答案 1 :(得分:0)

可能会对你有帮助(我不确定你想要输出的格式)

$datadump = '"Waived":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info",      "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';

print_r(explode(':',str_replace(',',':',$datadump)))

<强> 输出:

[0] => "Waived"
[1] => "Waived"
[2] =>  "Until"
[3] => " until"
[4] =>  "HeaderBanInfo"
[5] => "Ban Info"
[6] =>       "StatusLabel"
[7] => "Current Status
[8] => "
[9] =>  "StatusBanned"
[10] => "BANNED"
[11] =>  "StatusWarned"
[12] => "WARNED"
[13] =>  "StatusSuspended"
[14] => "SUSPENDED"
[15] =>  "StatusActive"
[16] => "ACTIVE"

答案 2 :(得分:0)

试试这个 $ pattern ='/ \“([a-zA-Z0-9] *?)\”:/ si';

答案 3 :(得分:0)

好的,因为你的问题不清楚你想要哪个名字。下面的代码显示了如何获取任何名称:

获取名字:

$datadump = '"Waived":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info", "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';

$pattern_for_1st_name = '/"([a-zA-Z:\s]*?)":/';

preg_match_all($pattern_for_1st_name, $datadump, $output);

var_dump($output[1]);

这将输出对中的名字:

array(8) { [0]=> string(6) "Waived" [1]=> string(5) "Until" [2]=> string(13) "HeaderBanInfo" [3]=> string(11) "StatusLabel" [4]=> string(12) "StatusBanned" [5]=> string(12) "StatusWarned" [6]=> string(15) "StatusSuspended" [7]=> string(12) "StatusActive" } 

获取第二个名字:

现在,如果你想要对中的第二个名字,下面就是它的模式:

 $pattern_for_2nd_name = '/:"([a-zA-Z:\s]*?)"/';

 preg_match_all($pattern_for_2nd_name, $datadump, $output);

 var_dump($output[1]);

这将输出第二个名字:

array(8) { [0]=> string(6) "Waived" [1]=> string(6) " until" [2]=> string(8) "Ban Info" [3]=> string(15) "Current Status:" [4]=> string(6) "BANNED" [5]=> string(6) "WARNED" [6]=> string(9) "SUSPENDED" [7]=> string(6) "ACTIVE" }

我不知道你的数据是否实际上有Current Status:字或冒号是拼写错误,所以也包括了匹配的冒号。

答案 4 :(得分:0)

由于您的输入字符串是JSON,因此您不需要使用preg_match_all,因此最好使用您的数据格式:

print_r(array_keys(json_decode('{' . $datadump . '}', true)));

PHP手册:
json_decode
array_keys

关于您的模式:
它不起作用,因为.*?允许任何类型的字符,包括"。正则表达式引擎将逐个字符地从左到右继续字符串,并将尽快给出匹配。这就是您的第二个结果是"Waived", "Until":

的原因

为了避免这些结果,一种方法是禁止"使用否定的字符类代替点:
/"([^"]+)":/ (注意使用这个,懒惰不再需要量词。)

答案 5 :(得分:0)

检查我的solution

json_decode('{'.$datadump.'}');

LOL