Re: Simple reading a file and extract fields

From: Date: Tue, 13 Nov 2007 16:31:24 +0000
Subject: Re: Simple reading a file and extract fields
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-264445@lists.php.net to get a copy of this message
Use myFile= file() which puts all your lines in a nice simple array. The use foreach()on the array to exact your sub-strings. Personally, I'd assemble the results in a big string. Then, echo the string. $fields= NULL; foreach(myFile as $line){ $fields .= "substr($line, 0, 5)" . "&nbsp; nbsp;" . "substr($line, 7, 46)" . "<br />\n\n"; } echo $fields; Ronald Wiplinger wrote:
I got a larger file which consists of lines with a defined length of 56 characters. Each line ends with a line feed (0A), From each line I want one field from position 1 ~ 5 and field 2 from position 7 ~ 46. I tried: $myFile = "plaiso"; $fh = fopen($myFile, 'r'); $theDataLine = explode("\n",fgets($fh));
echo "Field1    Field2<p>";     // headline
for($i=0;$i<count($theDataLine);$i++){
    $Field1 = substr($theDataLine[$i], 0, 5);
    $Field2 = substr($theDataLine[$i], 7, 46);
    echo "$Field1 &nbsp; &nbsp; $Field2";
} It prints only the headline and the first record. What do I miss? bye Ronald


« previous php.general (#264445) next »