Counting strings in a txt file

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: Counting strings in a txt file

Post by seandisanti » Tue Oct 09, 2012 1:14 pm

what about...

Code: Select all

    $txt_file = file_get_contents('./10M.txt');
    $pieces = explode(":", $txt_file);
    $pieces = array_map(checkit,$pieces);
	$passed = array_sum($pieces);
	$failed = count($pieces)-$passed;
    

	function checkit($value)
	{
		return ($value==987654321)?1:0;
	}

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Counting strings in a txt file

Post by egami » Tue Oct 09, 2012 1:50 pm

100 ways to skin this cat :)

Code: Select all

<?php

$passed = 0;
$failed = 0;

$file = get_file_contents('myFile.txt');
$lines = explode(':',$file);
foreach($lines as $line) {
 (($line == '987654321') ? $passed += 1 : $failed += 1);
}

echo "\r\n
Passed: $passed \r\n
Failed: $failed \r\n
";
 

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Counting strings in a txt file

Post by egami » Tue Oct 09, 2012 3:09 pm

Code: Select all

<?php

$passed = 0;
$failed = 0;
$letscount = 0;


$file = get_file_contents('myFile.txt');
$lines = explode(':',$file);

$total_count = count($lines);

foreach($lines as $line) {
 $letscount++;
 (($line == '987654321') ? $passed += 1 : $failed += 1);
}

echo "\r\n
Passed: $passed \r\n
Failed: $failed \r\n
Counted: $letscount \r\n
Total Lines: $total_lines\r\n 
";
  

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: Counting strings in a txt file

Post by seandisanti » Wed Oct 10, 2012 8:55 am

can you attach the file? i kind of want to test mine and see why it didn't work

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests