如何用php解析.plist文件?

时间:2023-02-06 12:35:28

Can i parse a plist file with php and kind of get it into an array, like the $_POST[''] so i could call $_POST['body'] and get the string that has the <key> body ?

我可以用PHP解析一个plist文件并将它变成一个数组,比如$ _POST ['']所以我可以调用$ _POST ['body']并获得具有 主体的字符串?

3 个解决方案

#1


22  

CFPropertyList - A PHP Implementation Of Apple's plist (PropertyList)

CFPropertyList - Apple的plist(PropertyList)的PHP实现

#2


1  

Googling for "php plist parser" turned up this blog post that seems to be able to do what you are asking for.

谷歌搜索“PHP plist解析器”发现这篇博文似乎能够做你想要的。

#3


0  

Took a look at some of the libraries out there but they have external requirements and seem overkill. Here's a function that simply puts the data in to associative arrays. This worked on a couple of exported itunes plist files I tried.

看看那里的一些图书馆,但他们有外部要求,看起来有点过分。这是一个简单地将数据放入关联数组的函数。这适用于我试过的几个导出的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;
}

#1


22  

CFPropertyList - A PHP Implementation Of Apple's plist (PropertyList)

CFPropertyList - Apple的plist(PropertyList)的PHP实现

#2


1  

Googling for "php plist parser" turned up this blog post that seems to be able to do what you are asking for.

谷歌搜索“PHP plist解析器”发现这篇博文似乎能够做你想要的。

#3


0  

Took a look at some of the libraries out there but they have external requirements and seem overkill. Here's a function that simply puts the data in to associative arrays. This worked on a couple of exported itunes plist files I tried.

看看那里的一些图书馆,但他们有外部要求,看起来有点过分。这是一个简单地将数据放入关联数组的函数。这适用于我试过的几个导出的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;
}