note 54376 added to function.fscanf
| From: | rudigreen at gmail dot com | Date: | Fri, 01 Jul 2005 22:43:56 +0000 |
| Subject: | note 54376 added to function.fscanf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91283@lists.php.net to get a copy of this message | ||
I have a function for reading delimited files, it works for multiple lines too (i think...)
<?
//$fh - is the file pointer
//$delim - is the seperator
//$callback - self explanatory
//$len - optional
function file_read_delim($fh,$delim,$callback,$len=1024)
{
$rec = '';
while(!feof($fh))
{
$buf = fread($fh,$len);
if(strpos($buf,$delim) === false)
{
$rec .= $buf;
}
else
{
$strs = explode($delim,$buf);
foreach ($strs as $ele)
{
$rec .= $ele;
call_user_func($callback,$rec);
$rec = '';
}
}
}
}
//Here is an example how to use the function
$fh = fopen($filename,'r');
if(!$fh)
{
die 'Could not open file for reading';
}
//call the function
file_read_delim($fh,'-','cb');
fclose($fh);
function cb($rec)
{
echo "$rec \n";
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.fscanf.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54376
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54376&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54376&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54376&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54376&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54376&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54376&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54376&report=yes
Search -- http://master.php.net/manage/user-notes.php