2012-10-20 92 views
-2

我想解析下列字符串到數組中。PHP用逗號和引號分析字符串

$str = [0,"Victoria Station, Bus Station Stand",null,null,0,null,51.496169,-0.143633] 

我使用

$result = explode(',', $str); 

和我越來越喜歡這個

Array ([0] => [0 [1] => "Victoria Station [2] => Bus Station Stand" [3] => null [4] => null [5] => 0 [6] => null [7] => 51.496169 [8] => -0.143633]) 

一個數組,但我需要「維多利亞火車站,汽車站站」是數組中的1項。我知道這可以通過正則表達式來實現。但我是新來的。你的指導將非常感謝。

回答

6

這看起來像json我嘗試

$str = '[0,"Victoria Station, Bus Station Stand",null,null,0,null,51.496169,-0.143633]'; 
$json = json_decode($str, true); 

echo "<pre>"; 
var_dump($json); 

輸出

array 
    0 => int 0 
    1 => string 'Victoria Station, Bus Station Stand' (length=35) 
    2 => null 
    3 => null 
    4 => int 0 
    5 => null 
    6 => float 51.496169 
    7 => float -0.143633 
+1

更好地使用'var_dump'或'var_export',而不是'print_r'。 – Gumbo

+0

@Gumbo忠告拍攝... :) – Baba

+1

@Baba謝謝!它像一個魅力!比我之前採用的正則表達式好得多。而你們這些人真快! – bluemachine

相關問題