简体   繁体   中英

Need help parsing some data in PHP

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

 Acct: 1 
 email       : bleh@gmail.com 
 status      : online 
 -------------------------------------------------- 

 Acct: 2 
 email       : dfgg@fgfg.com     
 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM