在PHP中解析带有标记的txt文件

pme*_*ino 2 php parsing text-files

我有一个.txt文件,如下所示:

Title: Test
Author: zad0xsis
Date: July 13th, 2011
Body: This is a test post and this can continue until the file end
Run Code Online (Sandbox Code Playgroud)

我怎样才能让PHP识别"标签"并将内容变为新的字符串?提前致谢!:d

Der*_*sed 5

$fc = file('some_file.txt'); // read file into array
foreach ($fc as $line) {
    list($tag, $content) = explode(':', $line, 2);
    // do something here
}
Run Code Online (Sandbox Code Playgroud)

现在,每个文件中有多个不相关的集合吗?如果是这样,你将不得不寻找一些标记,也许是一个新行,并进行重置.希望你可以自己解决这个问题.

一些功能供您查看:

编辑:略微扩展示例:

$fc = file('some_file.txt'); // read file into array
foreach ($fc as $index => $line) {
    list($tag, $content) = explode(':', $line, 2);
    // do something here
    if ('body' == strtolower($tag)) {
        $content = join(array_slice($fc, $index + 1, count($fc)));
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

更多功能为您服务!