2009-11-17 16 views

Respuesta

1

Google para "intérprete PHP plist" da como resultado this blog que parece ser capaz de hacer lo que está pidiendo.

0

Echa un vistazo a algunas de las bibliotecas, pero tienen requisitos externos y parecen excesivas. Aquí hay una función que simplemente coloca los datos en matrices asociativas. Esto funcionó en un par de archivos itunes plist exportados que probé.

// 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 ... ¿está utilizando expresiones regulares para intentar analizar XML? –

+0

Los analizadores xml ponen la clave/valor de la entrada plist como entidades separadas en la pista. esto los pone como matrices atribuidas a valores clave./shrug –

+0

Confía en que haya líneas nuevas y etiquetas xml específicamente formadas. –

Cuestiones relacionadas