RE: [PHP] counting number of lines in file() that match a regexp

From: Date: Sun, 16 Sep 2001 21:52:40 +0000
Subject: RE: [PHP] counting number of lines in file() that match a regexp
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-67540@lists.php.net to get a copy of this message
On 16-Sep-2001 Kurt Lieber wrote: > I'm trying to take a text file and count the number of lines that match > a regexp. I've got it working using the following code: > > $data = file("myfile.txt"); > $total = 0; > foreach($data as $i) { > if (ereg("^1,",$i)) { > $total = $total + 1; > } > } > > but I'm still learning PHP and am wondering if there's an easier/better > way to do this? > untested: $data = file("myfile.txt"); chop($data); $cnt=substr_count(join('~', $data), '~1,'); // change tilde to un-common char echo $cnt, '<br>'; or burn a few pids: $cnt=grep "^1," myfile.txt | wc -l; echo $cnt, '<br>'; Regards, -- Don Read dread@texas.net -- It's always darkest before the dawn. So if you are going to steal the neighbor's newspaper, that's the time to do it.

« previous php.general (#67540) next »