note 48420 deleted from function.file by bjori
| From: | bjori@php.net | Date: | Wed, 22 Mar 2006 13:49:24 +0000 |
| Subject: | note 48420 deleted from function.file by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-106254@lists.php.net to get a copy of this message | ||
Note Submitter: corychristison[-At]lavacube(dot.)com
----
The file function will load the entire file before you can do anything to it [remove lines,
explode() each line, and so on...]
I wrote a function [ file_v2() ] to help eliminate the restrictions of file(). Below are a few
features/abilites file_v2() has over file().
file_v2() features:
- uses an editable buffer size, for performance tweaking.
- ability set maximum lines returned[from beginning]
- optional automatically rtrim the line
- optional ability to provide a callback function for each array element
Here is the code:
<?php
function file_v2 ( $filename, $return_max_lines=0, $callback_func=null, $do_rtrim=true,
$buffer_size=1024 )
{
// open file pointer
$open = fopen( $filename, 'rb' );
// start an array [good coding practice]
$open_data = array();
// start an internal line counter [good coding practice]
$line=0;
// begin the loop
while( !feof($open) )
{
if( $do_rtrim )
{
// add line to array, do an rtrim()
$open_data[$line] = rtrim(fgets($open, $buffer_size));
}
else
{
// add line to array
$open_data[$line] = fgets($open, $buffer_size);
}
if( $callback_func != null && function_exists( $callback_func ) )
{
eval($callback_func . '($open_data[$line]);');
}
// +1 to the internal line counter
$line++;
if( $return_max_lines > 0 )
{
if( $line >= $return_max_lines )
{
break;
}
}
}
// close the file pointer
fclose($open);
// return the data
return $open_data;
}
?>
Example usage:
=========
file_test.txt
=========
01 firstname_1 lastname_1 fake_email@fake_addr1.fk
02 firstname_2 lastname_2 fake_email@fake_addr2.fk
03 firstname_3 lastname_3 fake_email@fake_addr3.fk
04 firstname_4 lastname_4 fake_email@fake_addr4.fk
05 firstname_5 lastname_5 fake_email@fake_addr5.fk
06 firstname_6 lastname_6 fake_email@fake_addr6.fk
07 firstname_7 lastname_7 fake_email@fake_addr7.fk
08 firstname_8 lastname_8 fake_email@fake_addr8.fk
09 firstname_9 lastname_9 fake_email@fake_addr9.fk
10 firstname_10 lastname_10 fake_email@fake_addr10.fk
11 firstname_11 lastname_11 fake_email@fake_addr11.fk
12 firstname_12 lastname_12 fake_email@fake_addr12.fk
=========
test.php
=========
<?php
// we will assume the function is in here somewhere
// create a callback function
function mk_callback( &$input )
{
$input = explode("\t", $input);
}
$open = file_v2('file_test.txt', 3, 'mk_callback');
// output array using print_r()
print_r($open);
?>
Example will output:
Array
(
[0] => Array
(
[0] => 01
[1] => firstname_1
[2] => lastname_1
[3] => fake_email@fake_addr1.fk
)
[1] => Array
(
[0] => 02
[1] => firstname_2
[2] => lastname_2
[3] => fake_email@fake_addr2.fk
)
[2] => Array
(
[0] => 03
[1] => firstname_3
[2] => lastname_3
[3] => fake_email@fake_addr3.fk
)
)
This is one of my few contributions to this fine world.
Happy coding everybody!
- Cory Christison