我需要从一个大数据包中提取名字。
$frame = '\"Amy Dardomba\":1,\"Kisb Muj Lorence\":1,\"Apkio Ronald\":1,....有超过200-300个名字我必须放在数组中。
我试过了
preg_match_all('#\/"(.*)\/":1#',$frame,$imn);
print_r($imn);但是它不起作用。请帮帮我。
谢谢
发布于 2012-03-28 20:44:43
在我看来,这些数据就像是一些混账的JSON。假设您的代码的格式与上面的完全相同,请尝试以下代码:
// Two pass approach to interpollate escape sequences correctly
$toJSON = '{"json":"{'.$frame.'}"}';
$firstPass = json_decode($toJSON, TRUE);
$secondPass = json_decode($firstPass['json'], TRUE);
// Just get the keys of the resulting array
$names = array_keys($secondPass);
print_r($names);
/*
Array
(
[0] => Amy Dardomba
[1] => Kisb Muj Lorence
[2] => Apkio Ronald
...
)
*/See it working
发布于 2012-03-28 20:44:10
\/将匹配/字符,但您需要匹配\,因此请改用\\:
preg_match_all('#\\"(.*?)\\":1#',$frame,$imn);还添加了一个用于非贪婪正则表达式的?。
发布于 2012-03-28 20:51:25
$input = '\"Amy Dardomba\":1,\"Kisb Muj Lorence\":1,\"Apkio Ronald\":1';
preg_match_all('#"([a-zA-Z\x20]+)"#', stripslashes($input), $m);在$m[1]中查找
https://stackoverflow.com/questions/9907529
复制相似问题