0

I have a script that parses a log file for a game (used for replays). It contains most of the actions in the game such as movements, kills, deaths, etc.

I've been using this script for a while but now I'm working on a stats site using Codeigniter and I'm getting a lot of error notices and I'm not sure why.

The exact error is Message: Undefined offset: 3

The lines it's referring to are the lines in the team case that have $message[3] in them.

Here's the script:

$f = fopen($full_path,"r");
    while ($line = fgets($f))
    {
        $line = str_replace("ööööö", " ",trim($line));  // these characters are found in win replay files
        $line = str_replace("ššššš", " ", $line);  // these characters are found in mac replay files
        $message = explode(" ",$line);
        $lasttimestamp = $message[sizeof($message)-1];
        if ($message[0]=="PLAYERSENT" || $message[0]=="TCPSENT")
        {
            array_shift($message);
            $swapfield = $message[0];
            $message[0] = $message[1];
            $message[1] = $swapfield;

        }
        switch ($message[0])
        {

        case "team":    // team (player id) (team)
            $name = $names[$message[1]];
            if ($message[2]>-1) $players[$name]['Played'] = true;

            $players[$name]['Sessions'][] = array("Team"=>$players[$name]['Team'],"TimeSpent"=>($message[3]-$players[$name]['LastTimeStamp']));
            if ($players[$name]['Team']>-1) $players[$name]['TotalTime'] += $message[3]-$players[$name]['LastTimeStamp'];
            $players[$name]['Team'] = $message[2];
            $players[$name]['LastTimeStamp'] = $message[3];
            break;
        default:
        }
    }
fclose($f);

The case matches lines that look like this:

team 4 -1ööööö57167168

As seen in the script, the ööööö characters are replaced with a space, so it should become team 4 -1 57167168, so $message[3] should be showing me the timestamp 57167168

I can't figure out why it's giving me the notices for this, any ideas how can fix this?

Motive
  • 3,071
  • 9
  • 40
  • 63
  • 1
    [`var_dump`](http://php.net/var_dump) `$message` in the case block. What does it show? – mAu Aug 12 '12 at 22:08
  • Check (e.g. as per @mAu's suggestion) that $message contains what you think it should after the explode. Since you are str_replace strings with accents you may be hitting encoding issues and hence finding the replacement is not actually taking place. – borrible Aug 12 '12 at 22:11
  • Ya, just did var_dump and it's lumping `-1ööööö57167168` into message[2] and not creating message[3]. It's showing `-1�����194511227` so it seems like an encoding issue. How can I get it to replace it correctly? – Motive Aug 12 '12 at 22:14
  • As [this answer](http://stackoverflow.com/a/1489555/773625) states, `�` is the unicode replace string. Which encoding is your input file? – mAu Aug 12 '12 at 22:26

0 Answers0