需要帮助解析 PHP 中的一些数据

发布于 2024-09-14 06:51:05 字数 1615 浏览 7 评论 0原文

我需要能够在 PHP 中解析此类数据:

 Acct: 1 
 email       : [email protected] 
 status      : online 
 -------------------------------------------------- 

 Acct: 2 
 email       : [email protected]     
 status      : banned 
 -------------------------------------------------- 

 Acct: 3 
 signedupname  : SomeUsername     
 somethingelse  : offline 
 -------------------------------------------------- 

如您所见,数据很大程度上是随机的。唯一保持不变的是分隔每个条目的 -----Acct: 1 位。每个 : 之间的填充经常发生变化,表示每一位数据的变量也会发生变化。

我尝试过使用正则表达式自己完成并解析所有内容,但我显然不够熟练,无法正确完成它。最后,我想要根据以下内容提供的数据:

Acct: <integer>
var1: <string>
var2: <string>

如果这有任何意义的话。它不需要那么有效,因为我需要每天做一次,而且我不介意等待它需要多长时间。

谢谢。 :)

编辑:这是我自己做的:

<?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;
}

?>

我已经提取了 Acct: 部分,但除此之外的任何事情我根本无法理解。

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

 Acct: 1 
 email       : [email protected] 
 status      : online 
 -------------------------------------------------- 

 Acct: 2 
 email       : [email protected]     
 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抽个烟儿 2024-09-21 06:51:05

这段代码将获取您的整个输入,并为输入中的每条记录生成一个关联数组:

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

编辑: 我刚刚查看了您发布的代码。您确实不需要正则表达式来完成您想要做的事情。当在正确的地方使用正则表达式时,它会非常方便,但大多数时候,它的使用并不正确。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文