note 34073 added to function.fscanf
| From: | anosh at kth dot se | Date: | Tue, 15 Jul 2003 15:03:42 +0000 |
| Subject: | note 34073 added to function.fscanf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-52140@lists.php.net to get a copy of this message | ||
//I felt the need for parsing an entire file, but I didn't want the data to be printed out at
the time of parsing.
//Instead I wanted the data to be stored for later use.
//
//Since I didn't find any examples I wrote a function my self.
//
//This is how the functions works:
//The call is made with two parameters.
// a) first one is equal to the name of the file you want to read
// b) second one is the number of parameters to parse.
//It returns an array (A) where each position is yet another array (B,C,D etc) equal to the line
number in the file starting from 0.
//The second-level arrays (B,C,D etc) contain alla parameters in a singel line as positions.
//ie:
// array A =
// [position 0 in A = array B ]
// [position 1 in A = array C ]
// [position 2 in A = array D ]
// where
// array B =
// [position 0 in B = parameter 1 on line 1 in the file]
// [position 1 in B = parameter 2 on line 1 in the file]
// [position 2 in B = parameter 3 on line 1 in the file]
// and
// array C =
// [position 0 in C = parameter 1 on line 2 in the file]
// [position 1 in C = parameter 2 on line 2 in the file]
// [position 2 in C = parameter 3 on line 2 in the file]
// etc..
//
// Another way of seeing it:
// array A = { B,C,D } = { B,
// C,
// D, }
// = { B={param1 line1, param2 line1, param3 line1},
// C={param1 line2, param2 line2, param3 line2},
// D={param1 line3, param2 line3, param3 line3} }
//
//
// |-Param1 line1
// The File(A) /-|-Param2 line1
// |--Pos1(B)-/ |-Param3 line1 |-Param1 line2
// |--Pos2(C)--------------------|-Param2 line2
// |--Pos3(D)-\ |-Param2 line3 |-Param3 line2
// .. \-|-Param2 line3
// . |-Param3 line3
// etc
//
// array A is returned.
// The function:
function getFile($filename,$no_of_parameters){
$separator_string = "%[^|]|";
$v=0;
$contents = array();
$line_of_parameters = array();
for($i=0; $i < $no_of_parameters ;$i++)
$separator_string .= "%[^|]|";
if($no_of_parameters==$i) //this doesn't seem to work..
$separator_string .= "\n";
$handle = fopen ("$filename","r");
while ($one_line_of_parameters = fscanf ($handle, $separator_string)) {
$contents[$v] = $one_line_of_parameters;
$v++;
}
fclose($handle);
return($contents);
}
// Example on how to retrieve data:
$a_file = getFile("Graphics.anjo",4);
foreach($a_file as $a_line){
foreach($a_line as $a_parameter)
echo "$a_parameter ";
echo "<\br>";
}
// The file being parsed, Graphics.anjo
Name|A good site|Updated|Added|url
anjothebanjo.com|yes|never|2003-04-17|http://www.anjothebanjo.com
----
Manual Page -- http://www.php.net/manual/en/function.fscanf.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34073
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34073&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34073&report=yes