在PHP中解析格式化的文本文件

时间:2023-02-08 07:17:27

Is there an easy way to parse the following data that I will post below. The data comes from the web.

有没有一种简单的方法来解析我将在下面发布的以下数据。数据来自网络。

I was using the $rows = explode("\n", $txt_file); then the $parts = explode('=', $line_of_text); to get the key name and values. However, I don't know how to handle the extra information that I do not want.

我使用$ rows = explode(“\ n”,$ txt_file);那么$ parts = explode('=',$ line_of_text);获取密钥名称和值。但是,我不知道如何处理我不想要的额外信息。

Additionally, I do not know how to get rid of the extra spaces. The file seems to be made for some kind of easy parsing. I have looked all over this site to find a solution. However, this data is quite different than the examples I have found on this site.

另外,我不知道如何摆脱额外的空间。该文件似乎是为了某种简单的解析。我已经遍布这个网站寻找解决方案。但是,这些数据与我在本网站上找到的示例完全不同。

# This file holds all the timelines available at this time.
# All lines starting with # is ignored by parser...
#

STARTINFO
description     =       Rubi-Ka 2
displayname     =       Rimor (Rubi-Ka 2)
connect         =       cm.d2.funcom.com
ports           =       7502
url             =       
version         =       18.5.4
ENDINFO

STARTINFO
description     =       Rubi-Ka 1
displayname     =       Atlantean (Rubi-Ka 1)
connect         =       cm.d1.funcom.com
ports           =       7501
url             =       
version         =       18.5.4
ENDINFO

1 个解决方案

#1


1  

You can use the trim function to get rid of the whitespace.

您可以使用trim函数来摆脱空白。

To only keep the columns you want, you can store their keys in an array, and make a check against it when parsing. Here's an example (albeit rather verbose).

要仅保留所需的列,可以将其键存储在数组中,并在解析时对其进行检查。这是一个例子(虽然相当冗长)。

<?
$lines = explode("\n", $data);
$result = array();
$count = 0;
// an array of the keys we want to keep
// I have the columns as keys rather then values for faster lookup
$cols_to_keep = array( 'url'=>null, 'description'=>null, 'ports'=>null, 'displayname' => null);

foreach($lines as $line)
{
  //skip comments and empty lines
  if(empty($line) || $line[0] == '#')
  {  continue; }

  //if we start a new block, initalize result array for it
  if(trim($line) == 'STARTINFO')
  {
    $result[$count] = array();
    continue;
  }

  // if we reach ENDINFO increment count
  if(trim($line) == 'ENDINFO')
  {
    $count++;
    continue;
  }

  //here we can split into key - value
  $parts = explode('=', $line);

  //and if it's in our cols_to_keep, we add it on
  if(array_key_exists(trim($parts[0]), $cols_to_keep))
  { $result[$count][ trim($parts[0]) ] = trim( $parts[1] );  }
}
print_r($result);
?>

#1


1  

You can use the trim function to get rid of the whitespace.

您可以使用trim函数来摆脱空白。

To only keep the columns you want, you can store their keys in an array, and make a check against it when parsing. Here's an example (albeit rather verbose).

要仅保留所需的列,可以将其键存储在数组中,并在解析时对其进行检查。这是一个例子(虽然相当冗长)。

<?
$lines = explode("\n", $data);
$result = array();
$count = 0;
// an array of the keys we want to keep
// I have the columns as keys rather then values for faster lookup
$cols_to_keep = array( 'url'=>null, 'description'=>null, 'ports'=>null, 'displayname' => null);

foreach($lines as $line)
{
  //skip comments and empty lines
  if(empty($line) || $line[0] == '#')
  {  continue; }

  //if we start a new block, initalize result array for it
  if(trim($line) == 'STARTINFO')
  {
    $result[$count] = array();
    continue;
  }

  // if we reach ENDINFO increment count
  if(trim($line) == 'ENDINFO')
  {
    $count++;
    continue;
  }

  //here we can split into key - value
  $parts = explode('=', $line);

  //and if it's in our cols_to_keep, we add it on
  if(array_key_exists(trim($parts[0]), $cols_to_keep))
  { $result[$count][ trim($parts[0]) ] = trim( $parts[1] );  }
}
print_r($result);
?>