Jump to content

Help Parsing text file


wee493

Recommended Posts

I need some help parsing this text file. I've tried a few things, but had trouble as it's not actually json data. I've tried various str_replaces and things along those lines, but I would like to be able to output this into a table with the Steam id, time, name, and reason. If someone can just point me in the right direction i'd be fine coding myself! Thanks!

"STEAM_0:0:18130940"	
{
"admin"	"(Console)"
"unban"	"1274151724"
"time"	"1273546924"
"name"	"-=SAS=- Death Master511"
"reason"	"Gcombat on spawn/Server Crash Attempts 1 week appeal at halania.com"
}
"STEAM_0:1:6428933"	
{
"time"	1273619777
"unban"	1273706177
"admin"	"TornadoChas3r(STEAM_0:0:19768725)"
"name"	".:T.¥:. TRÅÑŒ (CRYSTAL)"
"reason"	"RDM/ 1 day Ban \"You had Enough Warnings\" / :"
}

 

Here's basically what I have right now, which does not work too well :(

$myFile = "bans.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);

$rowsArr = explodeRows($theData);

for($i=0;$i<count($rowsArr);$i++) {
  $lineDetails = explodeTabs($rowsArr[$i]);
    echo "<br>Steam ID: " . $lineDetails[0];
    echo "<br>Surname : " . $lineDetails[1];
    echo "<br>Tel Number : " . $lineDetails[2];
    echo "<br><br>";
}

function explodeRows($data) {
  $rowsArr = explode("\n", $data);
  return $rowsArr;
}

// Explode the columns according to tabs
function explodeTabs($singleLine) {
  $tabsArr = explode("\t", $singleLine);
  return $tabsArr;
}

Link to comment
Share on other sites

If each block, eg

"STEAM_0:0:18130940"	
{
"admin"	"(Console)"
"unban"	"1274151724"
"time"	"1273546924"
"name"	"-=SAS=- Death Master511"
"reason"	"Gcombat on spawn/Server Crash Attempts 1 week appeal at halania.com"
}

is always going to occupy 8 lines. Then you could do something like this

<?php

function parseBans(&$dataArray)
{
    $bans = array();

    // split the array into chunks of 8
    // this is because each block occupies 8 lines
    // Example block
    /*
        "STEAM_0:0:18130940"	
        {
        "admin"	"(Console)"
        "unban"	"1274151724"
        "time"	"1273546924"
        "name"	"-=SAS=- Death Master511"
        "reason"	"Gcombat on spawn/Server Crash Attempts 1 week appeal at halania.com"
        }
    */
    $blocks = array_chunk($dataArray, ;

    // loop throught each ban block
    foreach($blocks as $block)
    {
        // get the steam id
        // matches "18130940" from "STEAM_0:0:18130940"
        preg_match('~[0-9]+)"~', $block[0], $m);
        $ban['steam_id'] = $m[1];
        
        // remove the "STEAM_0:0:xxxxxxx"", "{" and "}" lines
        unset($block[0], $block[1], $block[7]);
        
        // implode the $block array back into a string
        $bits = implode($block);
        
        //This now leaves us with the following data
        /*
            "admin"	"(Console)"
            "unban"	"1274151724"
            "time"	"1273546924"
            "name"	"-=SAS=- Death Master511"
            "reason"	"Gcombat on spawn/Server Crash Attempts 1 week appeal at halania.com"
        */
        // parse the above into "key" "value" pairs
        preg_match_all('~"([a-z]+)"\s+"(.*?)"~', $bits, $pieces, PREG_SET_ORDER);
        foreach($pieces as $piece)
        {
            list(,$key, $value) = $piece;
            $ban[$key] = $value;
        }
        
        // add the $ban array into the $bans array
        $bans[] = $ban;
    }
    
    // return the $bans array
    return $bans;
}

// read the bans.txt file into an array.
$arrayBans = file('bans.txt');

// run the function to parse the text file
$bans = parseBans($arrayBans);
?>

 

You'd parse the $bans array into a simple table using

echo '<table border="1" cellpadding="10">';
echo '<tr><th>' . implode('</h><th>', array_map('ucwords', array_keys($bans[0]))) . '</th></tr>';
foreach($bans as $ban):
    echo '<tr><td>' . implode('</td><td>', $ban) . '</td></tr>';
endforeach;
echo '</table>';

Link to comment
Share on other sites

One simple parsing structure is a loop like this:

 

$results = array();
$current_result = null;
foreach ($lineArr as $line) {
  if ($line == "}\n") {
    $results[] = $current_result;
    $current_result = null;
  }
  if (strpos($line, '"STEAM') === 0) {
    # Start of a new block
    $current_result['block_header'] = $line;
  }
  if (some condition to recognize a data line, like "admin" "(Console)") {
    # Parse the data line, and add a row like $current_result['admin'] = '(Console)'
  }
}

var_dump($results);

 

That's a sketch of how it will work.  It uses the "}" line as a marker to recognize when a block has finished, so it can add the results to the output array.

Link to comment
Share on other sites

or

<?php
$test ='"STEAM_0:0:18130940"	
{
"admin"	"(Console)"
"unban"	"1274151724"
"time"	"1273546924"
"name"	"-=SAS=- Death Master511"
"reason"	"Gcombat on spawn/Server Crash Attempts 1 week appeal at halania.com"
}
"STEAM_0:1:6428933"	
{
"time"	1273619777
"unban"	1273706177
"admin"	"TornadoChas3r(STEAM_0:0:19768725)"
"name"	".:T.¥:. TRÅÑŒ (CRYSTAL)"
"reason"	"RDM/ 1 day Ban \"You had Enough Warnings\" / :"
}
';
$test = preg_replace('/"(STEAM_[^"]+)"/', '[$1]', $test);
$test = preg_replace('/\{|}/', '', $test);
$test = preg_replace('/"([^"]+)"\t+([^ ])/S', '$1=$2', $test);

$test = parse_ini_string($test, 1);
print_r($test);
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.