3

如何在php中解析和显示下面的文本并在hmtl中输出?

我需要的是提示如何处理分隔列的空间。空格数不固定,所以我不能使用explode(" ",$string);而且我不确定下面输出的结构是否真的有固定宽度的列。我想让解析函数通用。

输出来自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 
4

4 回答 4

4

首先,有sscanf

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

它针对空格分隔值进行了优化,并且您已经可以指定变量类型(%s= string;%d= integer)(;甚至命名变量,但示例中未演示)。

示例/演示

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

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

var_dump($lines);

输出:

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"
  }
}
于 2012-06-06T15:54:17.210 回答
2

你可以preg_split按空间

$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
于 2012-06-06T07:13:05.527 回答
1

您可以preg_match像这样在每条数据线上使用:

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

1如果匹配,则返回,否则返回0。而且 - 更重要的是 - 中的列内容$matches,如下所示:

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',
)

该模式仅匹配由空白序列分隔的非空白序列。因此,只要数据本身没有空格,这应该适用于任何随机列长度。

于 2012-06-06T07:11:16.270 回答
1

这是一篇可以帮助你的文章。

//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.

您可以在本文中找到这些函数:http: //boulderapps.co/parsing-unevenly-spaced-columns-from-text-in-php

于 2014-08-23T07:29:08.363 回答