2

下面给出的数据格式为“name”:“Value”、“name2”:“Value 2”、“name3”:“Value 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);

但它不起作用。希望知道修复它的人会帮助我。谢谢你。

4

6 回答 6

0

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

于 2014-05-25T13:40:52.367 回答
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:单词或冒号是拼写错误,因此也包括匹配的冒号。

于 2014-05-25T13:52:49.220 回答
0

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

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

PHP 手册:
json_decode
array_keys

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

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

于 2014-05-25T14:21:14.710 回答
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"
于 2014-05-25T12:42:23.757 回答
0

检查我的解决方案

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

哈哈

于 2014-05-25T15:11:47.200 回答
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 */

编辑(仅适用于字母数字名称)

$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";
于 2014-05-25T12:29:42.887 回答