2009-11-17 98 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