Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
684 views
Welcome To Ask or Share your Answers For Others

1 Answer

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("
", $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"
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...