2009-11-17 11 views

Antwort

1

Googeln für „PHP-plist-Parser“ aufgedreht this Blog-Post, die in der Lage sein scheint zu tun, was Sie fordern.

0

Ich habe mir einige Bibliotheken angesehen, aber sie haben externe Anforderungen und scheinen übertrieben zu sein. Hier ist eine Funktion, die die Daten einfach in assoziative Arrays einfügt. Dies funktionierte auf ein paar exportierte iTunes plist-Dateien, die ich ausprobierte.

// 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

I ... verwendet dies reguläre Ausdrücke zum Testen und Parsen von XML? –

+0

XML-Parser setzen den Schlüssel/Wert des PLIST-Eintrags als separate Entitäten in der Spur. Dadurch werden sie als Arrays mit Schlüsselwert zugewiesen./shrug –

+0

Sie verlassen sich darauf, dass es neue Zeilen und speziell geformte XML-Tags gibt. –

Verwandte Themen