Forum Moderators: coopster

Message Too Old, No Replies

Reading a file line by line

         

MrSpeed

5:12 pm on Feb 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had a bit of code to read a file line by line looking for a match to a variable.

I was getting errors at the end of the while loop. It said something to the effect that one of the parameters for the strpos test was missing. I thought maybe I had some empty lines at the end of the file.

I was able to fix things by testing to see if the line is empty.

Does anyone have a guess why my original code was breaking? My last line break was on the same line as my last data point. So I did not have two line breaks next to each other at the end.

Is there a better way to rip through a file to test for a match?


$handle = fopen($_SERVER['DOCUMENT_ROOT']."/data.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle);
$pos = strpos(trim($buffer), trim($$name));
}

$handle = fopen($_SERVER['DOCUMENT_ROOT']."/data.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle);
if (strlen($buffer)>0){
$pos = strpos(trim($buffer), trim($$name));
}
}

jezra

5:38 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



I normally use file_get_contents(). it will read an entire file into a string. example:

$file = "PATH/TO/FILE.TXT";
$file_text = file_get_contents($file);
if( strstr($file_text,"jezra") )//if my name is in the file
{
echo "my name is in the file!";
}

horaghn

7:27 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



I created this sometime ago:

// ========================================================================
//=========================================================================
// NAME: find_string
// DATE CREATED: 05/31/2004
// DATE MODIFIED:
// PURPOSE:
//
// ========================================================================
// ========================================================================
function find_string($cString,$cRegex) {
$aFound = array();
preg_match( "/$cRegex/", $cString, $aFound );
return $aFound[1];
}

ironik

12:14 am on Feb 24, 2005 (gmt 0)

10+ Year Member



strpos() is another way to find out whether a string exists in a sub string, especially if you only need to find one match and need something that is relatively fast (regex's are great if you need the extra power, but can be a little slow).

[example]
$file = 'file.txt'
$handle = @fopen($file, 'r');
$contents = @fread($handle, filesize($file));
$strPos = strpos('text to search', $contents);
if ($strPos!== false)
{
echo 'Found Text!';
}
fclose($handle);
[/example]

strpos() returns the postion of the first character in the string if it finds a match. If it finds no matches, then the if statement will evaluate to false or 0 (I think).

jshpro2

2:16 am on Feb 24, 2005 (gmt 0)

10+ Year Member



If you need to find the specific line do this:

$var=file_get_contents("file");
$var=explode("\n", $var);
foreach ($var as $line=>$data) {
if (preg_match("/word/", $data) {
echo ($line.' contains \'word\'');
}
}