我可以用php解析一个plist文件并将其放入一个数组中,就像$_POST['']
我可以调用$_POST['body']
并获取具有的字符串<key> body
吗?
问问题
11881 次
3 回答
22
于 2009-11-17T15:29:21.357 回答
1
谷歌搜索“php plist parser”出现了这篇博客文章,它似乎能够满足你的要求。
于 2009-11-17T15:31:26.110 回答
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;
}
于 2016-02-12T21:32:18.477 回答