Php 读取文本文件的某些行
我必须为txt文件编写一个解析器,其结构如下:Php 读取文本文件的某些行,php,parsing,fread,Php,Parsing,Fread,我必须为txt文件编写一个解析器,其结构如下: $c = 0; if (fread($file, 1) == "a" && $c == 0) $c++; if (fread($file, 1) == "n" && $c == 1) $c++; if (fread($file, 1) == "o" && $c == 2) $c++; // And here after I check if
$c = 0;
if (fread($file, 1) == "a" && $c == 0) $c++;
if (fread($file, 1) == "n" && $c == 1) $c++;
if (fread($file, 1) == "o" && $c == 2) $c++;
// And here after I check if this is correct line, I take the number and write the rest of it to output.txt
<?php
$data = file_get_contents($filename);
$entries = explode(",", $data);
foreach($entries as $entry) {
if(strpos($entry, "anotherExample") === 0) {
//Split the entry into label and value, then print the value.
}
}
?>
某物的示例:95428,另一个示例:129,您需要这个:491,\n
另一个例子:30219,某物的例子:4998,你需要这个:492
但是有一个主要的问题-就像在这个例子中-文件并不总是以一个顺序出现,有时我在“anotherExample”之前会得到“youNeedThis”等等,但是结构
{variable}:{value}
总是一样的。我知道我在寻找什么(即,我只想读取“anotherExample”的值)。当我得到这个数字时,我希望它以单独的行将其写入某个txt文件:
129
30219
到目前为止,我得到的是将文件中的每个数字写在单独的行中,但我必须将它们过滤掉,以便只包含我要查找的数字。有没有一种方法可以过滤掉这些信息,而不必执行以下操作:
$c = 0;
if (fread($file, 1) == "a" && $c == 0) $c++;
if (fread($file, 1) == "n" && $c == 1) $c++;
if (fread($file, 1) == "o" && $c == 2) $c++;
// And here after I check if this is correct line, I take the number and write the rest of it to output.txt
<?php
$data = file_get_contents($filename);
$entries = explode(",", $data);
foreach($entries as $entry) {
if(strpos($entry, "anotherExample") === 0) {
//Split the entry into label and value, then print the value.
}
}
?>
发现
发现
像这样的怎么样:
$c = 0;
if (fread($file, 1) == "a" && $c == 0) $c++;
if (fread($file, 1) == "n" && $c == 1) $c++;
if (fread($file, 1) == "o" && $c == 2) $c++;
// And here after I check if this is correct line, I take the number and write the rest of it to output.txt
<?php
$data = file_get_contents($filename);
$entries = explode(",", $data);
foreach($entries as $entry) {
if(strpos($entry, "anotherExample") === 0) {
//Split the entry into label and value, then print the value.
}
}
?>
您可能想做一些比只进行
分解
更健壮的操作来获取$entries
,比如preg\u split这样的操作怎么样:
$c = 0;
if (fread($file, 1) == "a" && $c == 0) $c++;
if (fread($file, 1) == "n" && $c == 1) $c++;
if (fread($file, 1) == "o" && $c == 2) $c++;
// And here after I check if this is correct line, I take the number and write the rest of it to output.txt
<?php
$data = file_get_contents($filename);
$entries = explode(",", $data);
foreach($entries as $entry) {
if(strpos($entry, "anotherExample") === 0) {
//Split the entry into label and value, then print the value.
}
}
?>
您可能想做一些比只进行分解
更健壮的操作来获取$entries
,比如preg\u split我已经解决了这个问题:
$fileHandlerInput = file_get_contents($fileNameInput);
$rows = explode (",", $fileHandlerInput);
foreach($rows as $row) {
$output = explode(":", $row);
if (preg_match($txtTemplate, trim($output[0]))) {
fwrite($fileHandlerOutput[0], trim($output[1])."\r");
}
}
这不是最有效、最简洁的方法,但它很有效,两个答案都帮助我解决了这个问题。我用以下方法解决了这个问题:
$fileHandlerInput = file_get_contents($fileNameInput);
$rows = explode (",", $fileHandlerInput);
foreach($rows as $row) {
$output = explode(":", $row);
if (preg_match($txtTemplate, trim($output[0]))) {
fwrite($fileHandlerOutput[0], trim($output[1])."\r");
}
}
这不是最有效也最简洁的一个,但它很有效,两个答案都帮助我解决了这个问题。是的,或者你可以像Alma do Mundo在他们的答案中建议的那样用正则表达式完成整个过程。是的,或者你可以像Alma do Mundo在他们的答案中建议的那样用正则表达式完成整个过程。