2009-11-17 79 views

回答

1

谷歌搜索「PHP解析器的plist」止跌回升this博客文章,這似乎是能夠做你所要求的。

0

看了看一些庫在那裏,但他們有外部的要求,似乎矯枉過正。這是一個簡單地將數據放入關聯數組的函數。這對我嘗試過的幾個導出的iTunes plist文件起作用。

// pass in the full plist file contents 
function parse_plist($plist) { 
    $result = false; 
    $depth = []; 
    $key = false; 

    $lines = explode("\n", $plist); 
    foreach ($lines as $line) { 
     $line = trim($line); 
     if ($line) { 
      if ($line == '<dict>') { 
       if ($result) { 
        if ($key) { 
         // adding a new dictionary, the line above this one should've had the key 
         $depth[count($depth) - 1][$key] = []; 
         $depth[] =& $depth[count($depth) - 1][$key]; 
         $key = false; 
        } else { 
         // adding a dictionary to an array 
         $depth[] = []; 
        } 
       } else { 
        // starting the first dictionary which doesn't have a key 
        $result = []; 
        $depth[] =& $result; 
       } 

      } else if ($line == '</dict>' || $line == '</array>') { 
       array_pop($depth); 

      } else if ($line == '<array>') { 
       $depth[] = []; 

      } else if (preg_match('/^\<key\>(.+)\<\/key\>\<.+\>(.+)\<\/.+\>$/', $line, $matches)) { 
       // <key>Major Version</key><integer>1</integer> 
       $depth[count($depth) - 1][$matches[1]] = $matches[2]; 

      } else if (preg_match('/^\<key\>(.+)\<\/key\>\<(true|false)\/\>$/', $line, $matches)) { 
       // <key>Show Content Ratings</key><true/> 
       $depth[count($depth) - 1][$matches[1]] = ($matches[2] == 'true' ? 1 : 0); 

      } else if (preg_match('/^\<key\>(.+)\<\/key\>$/', $line, $matches)) { 
       // <key>1917</key> 
       $key = $matches[1]; 
      } 
     } 
    } 
    return $result; 
} 
+0

我...這是使用正則表達式,試圖解析XML? – 2016-02-12 21:35:20

+0

xml解析器將plist項的鍵/值作爲單獨的實體放置在軌道中。這將它們作爲關鍵值賦予數組。 /聳聳肩 – 2016-02-13 19:25:13

+0

你依靠有新行和專門形成的XML標籤。 – 2016-02-13 19:56:05