parsing - Reading certain lines of a textfile PHP -
i have write parser txt file structure this:
exampleofsomething: 95428, anotherexample: 129, youneedthis: 491,\n
anotherexample: 30219, exampleofsomething: 4998, youneedthis: 492,
but there 1 major problem - in example - file doesn't come out in 1 order, "youneedthis" before "anotherexample" etc., structure
{variable}: {value},
is same. know i'm looking (i.e. want read value of "anotherexample"). when number want write txt file in separate lines:
129
30219
from i've gotten far write every number file in separate line, have filter them out contain ones i'm looking for. there way of filtering out without having this:
$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++; // , here after check if correct line, take number , write rest of output.txt
how this:
<?php $data = file_get_contents($filename); $entries = explode(",", $data); foreach($entries $entry) { if(strpos($entry, "anotherexample") === 0) { //split entry label , value, print value. } } ?> you'll want little more robust explode $entries, preg_split.
Comments
Post a Comment