views:

47

answers:

1

I need to be able to parse this sort of data in PHP:

 Acct: 1 
 email       : [email protected] 
 status      : online 
 -------------------------------------------------- 

 Acct: 2 
 email       : [email protected]     
 status      : banned 
 -------------------------------------------------- 

 Acct: 3 
 signedupname  : SomeUsername     
 somethingelse  : offline 
 -------------------------------------------------- 

As you can see the data is largely random. The only thing that remains the same is the ----- seperating each entry and the Acct: 1 bits. The padding between each : often changes as does the variable to represent each bit of data.

I've tried going through and parsing it all myself using regex but I am defenately not skilled enough to be able to do it properly. At the end of this I want data according to the following:

Acct: <integer>
var1: <string>
var2: <string>

If that makes any sense at all. It doesn't need to be that effeciant, as I will need to do this about once a day and I do not mind waiting for how-ever long it needs.

Thank you. :)

Edit: This is what I did on my own:

<?php

$data = file_get_contents('records.txt');
$data = explode('******** Saved Host list with acct/variables ********', $data);
$data = explode('--------------------------------------------------', $data[1]);

foreach($data as &$dataz)
{
    $dataz = trim($dataz);
}

$data = str_replace('Acct:', "\nAcct:", $data);

foreach($data as $dataz)
{
    preg_match('/Acct: (.*)/', $dataz, $match);
    $acct = $match[1];
    preg_match('/: (.*)/', $dataz, $match);
    $var1 = $match[1];
    echo $var1;
}

?>

I got as far as extracting the Acct: part, but anything beyond that I simply can't get my head around.

+1  A: 

This piece of code will take your entire input and produce an associative array for each record in your input:

// replace ----- with actual number of dashes
foreach (explode('-----', $input) as $entry) {
  $entry = trim($entry);
  $record = array();
  foreach (explode("\n", $entry) as $line) {
    $parts = explode(':', $line);
    $varname = trim($parts[0]);
    $value = trim($parts[1]);
    $record[$varname] = $value;
  }
  // Do anything you want with $record here
}

Edit: I just had a look at the code you posted. You really don't need regular expressions for what you're trying to do. Regex can be really handy when used in the right place, but most of the time, it's not the right thing to use.

casablanca
Excellent, thanks. Only problem I have is sometimes the account has a URL for example `http://google.com`, which your block of code explodes by thus making the output weird. But I can more than likely fix that myself.Thank you! :)
Kratz
You can pass a limit parameter to `explode`, just make it `explode(':', $line, 2)` and it'll only split it into 2 parts.
casablanca