2012-03-28 32 views
0

我需要從大數據包中提取名稱。指定字符的模式

$frame = '\"Amy Dardomba\":1,\"Kisb Muj Lorence\":1,\"Apkio Ronald\":1,.... 

有超過200-300名我必須放在數組中。

我想,

preg_match_all('#\/"(.*)\/":1#',$frame,$imn); 
print_r($imn); 

但它不工作。請幫幫我 。

感謝

回答

1

該數據看起來像一些混蛋的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

0

\/將匹配/字符,但你需要匹配\所以使用\\代替:

preg_match_all('#\\"(.*?)\\":1#',$frame,$imn); 

同時加入了一個?非貪婪的正則表達式。

0
$input = '\"Amy Dardomba\":1,\"Kisb Muj Lorence\":1,\"Apkio Ronald\":1'; 
preg_match_all('#"([a-zA-Z\x20]+)"#', stripslashes($input), $m); 

看在$m[1]