我需要将一个文本文件解析成php数组。这是我的文本文件:
file: slide1.jpg | title: Title here | description: Aenean eleifend ultrices leo at venenatis. Suspendisse luctus | crop: top
file: slide2.jpg | description: Phasellus ac tortor ut dolor blandit tincidunt | title: Nullam cursus | crop: bottom
file: slide3.jpg | title: Hendrerit lacinia nisl | description: Tortor ut dolor blandit tincidunt | crop: bottom
file: slide4.jpg | title: Morbi hendrerit lacinia nisl | description: Maecenas venenatis lectus vitae | crop: left
我想把它解析成这样的结构化数组:
array(4) {
"slide1.jpg" => array (
"title" => "Title here",
"description" => "Aenean eleifend ultrices leo at venenatis. Suspendisse luctus",
"crop" => "top"
),
"slide2.jpg" => array (
"title" => "Nullam cursus",
"description" => "Phasellus ac tortor ut dolor blandit tincidunt",
"crop" => "top"
),
"slide3.jpg" => array (
"title" => "Hendrerit lacinia nisl",
"description" => "Tortor ut dolor blandit tincidunt",
"crop" => "top"
),
"slide4.jpg" => array (
"title" => "Morbi hendrerit lacinia nisl",
"description" => "Maecenas venenatis lectus vitae",
"crop" => "top"
)
}
我尝试了许多重复的foreach语句,但效率不高,而且代码变得非常冗长。有没有人知道如何更简单地实现它。
答案 0 :(得分:4)
首先:小心!
这可能是毛茸茸的事情,有很多可能的例外。我提供的解决方案确实:
|
,这会使这个东西绊倒。另一方面,值可以安全地包含冒号。"file"
始终存在。在盲目复制/粘贴之前考虑这些注意事项! ;)
此外,我的解决方案在每个元素中都包含文件名,这是多余的。但是删除它会使解决方案变得更加混乱而没有太大的价值。
这是一个解决方案:
<?php
/**
* Parse a line of the file. Returns an associative array, using the part
* before the colon as key, the following part as value.
*
* @param $line A line of text.
*/
function parse_line($line) {
// split on each '|' character.
$fields = explode('|', $line);
$data = array();
foreach($fields as $field) {
// unpack key/value from each 'key: value' text. This will only split on
// the first ":", so the value may contain colons.
list($key, $value) = explode(':', $field, 2);
// remove surrounding white-space.
$key = trim($key);
$value = trim($value);
$data[$key] = $value;
}
return $data;
}
/**
* Parses a file in the specified format.
*
* Returns an associative array, where the key is a filename, and the value is
* an associative array of metadata.
*
* @param $fname The filename
*/
function parse_file($fname) {
$handle = fopen($fname, "r");
$lines = array();
if ($handle) {
while (($line = fgets($handle)) !== false) {
$data = parse_line($line);
$lines[$data["file"]] = $data;
}
} else {
// error opening the file.
}
return $lines;
}
var_dump(parse_file("testdata.txt"));
答案 1 :(得分:1)
以下应该可以解决问题。
$rows = array();
foreach (preg_split('#\n#', file_get_contents('blah.txt')) as $line) {
if (preg_match_all('#([^"|]+)\s*:\s*([^|]+)#', $line, $parts)) {
$properties = array_map('trim', $parts[1]);
$values = array_map('trim', $parts[2]);
assert(count($properties) == count($values));
$row = array();
foreach ($properties as $index => $propertyName) {
$row[$propertyName] = $values[$index];
}
$rows[] = $row;
}
}
var_dump($rows);
答案 2 :(得分:-1)
尝试:
$new_array = array();
while (($data = fgetcsv($csvfile, 1000, ";")) !== FALSE) {
$new_array[$data[0]] = array('title' => $data[1], 'description' => $data[2], 'crop' => $data[3]);
}
var_dump($new_array);