PDA

View Full Version : search text file and print which line it occurs on


redhead
10-13-2002, 02:18 PM
hello. ive been doing a little (make that... very little) php for a while now... but i need some help...

i need to search through a text file for a string... and if its there it needs to print which line it is on.

ive used something similar to this to search a file in the past...$array = file("example.txt");
if (in_array("What I'm trying to find",$array)) {
print "What I'm trying to find is in the file";
}but it needs to print which line it finds the string on.

thanks in advance... :thumbsup:

firepages
10-13-2002, 02:35 PM
<?
$yaks=array_keys($file,"what I want to find\n");
echo $yaks[0];
?>



note the "\n" at the end of the string -this may be \r or \n or even \r\n depending on the format of the file, else you will have to trim() all the lines in $file() first.

also if there are several lines with the same value they will be $yaks[1] ,$yaks[2] etc.

redhead
10-13-2002, 02:39 PM
thanks. i thought there would be a simple enough answer :cool: