我需要将文本文件解析为php数组。这是我的文本文件:将格式化文本文件解析为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语句尝试,但它不是那么有效,代码变得非常冗长。有没有人知道一种更简单的方法。
在文本文件中的内容没有被格式化为CSV和“列”不用排队。这些文本文件是否以某种方式生成? –
这不是一个CSV文件。至少,这可能是'| SV'。 –
csv文件可以用任何东西分隔,它不一定是逗号。我使用|作为分隔符,因为逗号是常见的文本字符。它被手动编辑/维护以便于结构。新行定义了一个新项目,管道将属性分开。 – Cruising2hell