0

I have this php code, this I have allready tried:

<?php 
foreach (glob("somefolder/*/*/wdl.txt") as $somevar) {include $somevar;} 
?>

and this code output this:

WIN DRAW LOSE WIN WIN DRAW WIN DRAW

Now i want make this output variables that tells how many win i have, how many draw, and how many lose.

for example the variable when I echo the $wins variable will have output 4 becouse of 4 WIN.

How I can do that? thx.

each wdl.txt conteins only LOSE or DRAW or WIN . BUT!! I think contains: ''LOSE '' (with a space at the END).

user2773673
  • 39
  • 1
  • 5
  • 2
    Increment a counter in your loop. Output the counter after your loop. – crush Sep 14 '13 at 12:35
  • Possible duplicate: http://stackoverflow.com/questions/4103287/read-a-plain-text-file-with-php – Sumurai8 Sep 14 '13 at 12:36
  • 1
    Just to clarify, each `wdl.txt` is literally just a plain-text file with the string WIN, LOSE or DRAW right? as opposed to some horrendous setup of php scripts which are returning that value based on some other conditions? – Emissary Sep 14 '13 at 12:46
  • yes, each wdl.txt conteins only LOSE or DRAW or WIN. BUT!! i think contains: ''LOSE '' (with a space at the END). – user2773673 Sep 14 '13 at 13:03

1 Answers1

-2

Here's a way to do it, with the following in mind, Read a plain text file with php

<?php
$foo = '';
foreach (glob("somefolder/*/*/wdl.txt") as $somevar) {
  //If required: include $somevar;
  $foo .= file_get_contents($somevar);
}

if (!empty($foo)) {
    $wins = preg_match_all('~WIN~iUs', $foo, $matches);
    $draw = preg_match_all('~DRAW~iUs', $foo, $matches);
    $lose = preg_match_all('~LOSE~iUs', $foo, $matches);

    print 'Wins: ' . $wins;
    print 'Draw: ' . $draw;
    print 'Lose: ' . $lose;
}
?>

And another option.

<?php
/**
 * Results class.
 */
class Result
{
    /**
     * Magic method set propertie.
     */
    public function __set($name, $value)
    {
        $this->$name = $value;
    }

    /**
     * Magic method get propertie.
     */
    public function __get($name)
    {
        return $this->$name;
    }
}

//Variables.
$stat   = '';
$result = new Result();
$path   = getcwd() . '/*.txt';

//Loop trough $path files.
foreach (glob($path) as $file) {
    //Get the content from the stat file.
    $stat = file_get_contents($file);

    //If the file whith stat is empty, continue to next file.
    if (empty($stat)) {
        continue;
    }

    //Check if we already have a stat result in object.
    if (isset($result->$stat)) {
        //Add it up.
        $result->$stat++;
    } else {
        //Create the stat and asign 1 to it.
        $result->$stat = 1;
    }   
}

/**
 * Get results manually.
 */
print 'Win: ' . $result->win . '<br>';
print 'Lose: ' . $result->lose . '<br>';
print 'Draw: ' . $result->draw . '<br>';

/**
 * Or trough loop.
 */
//Get all properties from the Result object.
$results = get_object_vars($result);

//Loop trough the properties and print the values.
foreach($results as $key => $value) {
    print $key . ': ' . $value . '<br>';
}
Community
  • 1
  • 1
Michal
  • 1,010
  • 2
  • 8
  • 18
  • `$somevar` is a path to a filename – Sumurai8 Sep 14 '13 at 12:42
  • thx, but this code outputs a blank page, meaby contain a bug? i dont know if this happent but, check it if you can. thx. – user2773673 Sep 14 '13 at 13:00
  • the same (I have also include html above the code, but still blank page, so the code still have an error somewhere. – user2773673 Sep 14 '13 at 13:08
  • @MichalSkrzypecki syntax error after `file_get_contents` too many `)` - you should perhaps run your own code before posting it hastily... it's a good idea to discourage bad practices too - if you are reading the file anyway you shouldn't be using `include` as well, this could lead to some horrible problems. Also, three seperate regex patterns is just lazy :/ – Emissary Sep 14 '13 at 13:12
  • i remove the last **)** from file_get_contents and is now working but i dont want to include the output of the words, only to count them. thx, i solve my proble by just removing the line **include $somevar;**. – user2773673 Sep 14 '13 at 13:15
  • i have edit your post, and now its working, when accepted the edit, I can accept your Answer. – user2773673 Sep 14 '13 at 13:24
  • I don't know how to accept an edit from someone else. Not so long active on SO. But i have edited the answer and given an additional option. Just to show the other that i have put time in it ;) – Michal Sep 14 '13 at 16:42