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
$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)
现在,每个文件中有多个不相关的集合吗?如果是这样,你将不得不寻找一些标记,也许是一个新行,并进行重置.希望你可以自己解决这个问题.
一些功能供您查看:
filefile_get_contentsexplodelist (不是真正的功能)$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)
更多功能为您服务!
strtolowerjoin(又名implode)array_slicetrim- 我的解决方案中没有使用它,但您可能希望使用它来修改返回的行末尾的换行符file().或者,您可以FILE_IGNORE_NEW_LINES在调用时使用该标志file(),有关该标志的更多信息可以在PHP手册条目中找到file()(也在上面链接).