Re: counting number of lines in file() that match a regexp

From: Date: Fri, 05 Oct 2001 13:55:49 +0000
Subject: Re: counting number of lines in file() that match a regexp
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-70105@lists.php.net to get a copy of this message
Hello, $content = file('myfile.txt'); $total = count(preg_grep('|^1,|', $content)); BTW: if you are fan of regular expressions, you may try the PERL compatible regular functions, like: preg_grep. These ones are more powerful than simple ereg*() functions. ps.: sorry for the late response. ----- Original Message ----- From: "Kurt Lieber" <php@kirpatrick.com> To: <php-general@lists.php.net> Sent: Sunday, September 16, 2001 8:20 PM Subject: [PHP] counting number of lines in file() that match a regexp > 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? > > Thanks. > > --kurt > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#70105) next »