How to parse fixed width column text in php?

2020-02-07 03:41发布

问题:

How to parse and display below text in php and output it in hmtl?

What I need is a hint how to approach the spaces that separates the columns. The number of spaces is not fixed so I cannot use explode(" ",$string); And also I am not sure if the structure of below output has really fixed width columns. I want to make the parsing function generic.

The output comes from db2 list applications

Auth Id  Application    Appl.      Application Id                                                 DB       # of
         Name           Handle                                                                    Name    Agents
-------- -------------- ---------- -------------------------------------------------------------- -------- -----
DB2INST1 db2jcc_applica 11446      10.0.0.209.51406.120606004531                                  WEI      1    
DB2INST1 db2jcc_applica 11448      10.0.0.209.51407.120606004536                                  WEI      1    
DB2INST1 db2jcc_applica 13762      10.0.0.206.57473.120606024909                                  DOM_BUGS 1    
ADMIN    db2jcc_applica 15220      10.0.0.210.52248.120606045402                                  RATIONAL 1    
DB2INST1 php-fpm: pool  16546      127.0.0.2.35530.120606065726                                   KON      1    
DB2INST1 db2jcc_applica 16547      10.0.0.202.52042.120606065813                                  KON      1 

回答1:

You can preg_split by space

$words = preg_split("/[\s]+/", $input);

//if your lines seperated by `\n` new line you could:
$inputArr = preg_split("/\R+/", $input);
foreach($inputArr as $value) {
   $out = preg_split("/\s+/", $value);
   var_dump($out);
}

Thanks


回答2:

First of all, there was sscanf:

$vars = sscanf($string, '%s %s %d %s');

It is optimized for whitespace separated values, and you can specify the variable type already (%s = string; %d = integer)(; even name the variables but that is not demonstrated in the example).

Example/Demo:

$lines = explode("\r\n", $input);

foreach($lines as &$line)
{
    $line = sscanf($line, '%s %s %d %s');
}

var_dump($lines);

Output:

array(6) {
  [0]=>
  array(4) {
    [0]=>
    string(8) "DB2INST1"
    [1]=>
    string(14) "db2jcc_applica"
    [2]=>
    int(11446)
    [3]=>
    string(29) "10.0.0.209.51406.120606004531"
  }
  [1]=>
  array(4) {
    [0]=>
    string(8) "DB2INST1"
    [1]=>
    string(14) "db2jcc_applica"
    [2]=>
    int(11448)
    [3]=>
    string(29) "10.0.0.209.51407.120606004536"
  }
  [2]=>
  array(4) {
    [0]=>
    string(8) "DB2INST1"
    [1]=>
    string(14) "db2jcc_applica"
    [2]=>
    int(13762)
    [3]=>
    string(29) "10.0.0.206.57473.120606024909"
  }
  [3]=>
  array(4) {
    [0]=>
    string(5) "ADMIN"
    [1]=>
    string(14) "db2jcc_applica"
    [2]=>
    int(15220)
    [3]=>
    string(29) "10.0.0.210.52248.120606045402"
  }
  [4]=>
  array(4) {
    [0]=>
    string(8) "DB2INST1"
    [1]=>
    string(8) "php-fpm:"
    [2]=>
    NULL
    [3]=>
    NULL
  }
  [5]=>
  &array(4) {
    [0]=>
    string(8) "DB2INST1"
    [1]=>
    string(14) "db2jcc_applica"
    [2]=>
    int(16547)
    [3]=>
    string(29) "10.0.0.202.52042.120606065813"
  }
}


回答3:

You could use preg_match on each data line as such:

preg_match('~^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$~', $line, &$matches);

This would return 1 if matched, 0 if not. And - more importantly - the column content in $matches, like the following:

array (
  0 => 'DB2INST1 db2jcc_applica 11446      10.0.0.209.51406.120606004531',
  1 => 'DB2INST1',
  2 => 'db2jcc_applica',
  3 => '11446',
  4 => '10.0.0.209.51406.120606004531',
)

The pattern simply matches sequences of non-whitespace separated by sequences of whitespace. So as long as there is no whitespace as part of the data itself, this should work on any random column length.



回答4:

Here is an article that will help you.

//To parse an example like the following categorized columns:
/*
col1          col2    col3
====          ====    ====
1 a b c d e   103     14 as d9
2 a           103     14 as d9
3 a           103     14 as d9
*/



$headings = array('col1','col2','col3');
$header = "col1 col2 col3";
//get the $heading_pos_list by parsing the headings of each column with
list($heading_pos_list, $lengths) = parse_heading($headings, $header);
//Parse each line into a row structure
$start_position = $heading_pos_list[0][$heading_key];
$length_of_heading = $heading_pos_list[1][$heading_key];
$line = '1 a b c d e 103 14 as d9';
$row = parse_line($line, $headings, $start_position, $length_of_heading);
//this works for each column and line
echo $row('col1');
//output:
//1 a b c d e
//continue on for each line.

You can find these functions in this article here: http://boulderapps.co/parsing-unevenly-spaced-columns-from-text-in-php



标签: php parsing