0

我有一个看起来像这样的 PHP数组

array(10) {
  [1]=>
  string(69) "2013-06-12 11:25:44 [INFO] There are no objectives on the scoreboard"

  [2]=>
  string(53) "2013-06-12 11:42:27 [INFO] [Server] Hi, how are you?"

  [3]=>
  string(86) "2013-06-12 11:43:40 [INFO] Usage: /scoreboard objectives <list|add|remove|setdisplay>"

  [4]=>
  string(95) "2013-06-12 11:57:51 [INFO] /scoreboard objectives add <name> <criteriaType> [display name ...]"

  [5]=>
  string(67) "2013-06-12 11:57:59 [INFO] Added new objective 'test' successfully"

  [6]=>
  string(64) "2013-06-12 11:58:16 [INFO] Showing 3 objective(s) on scoreboard"

  [7]=>
  string(74) "2013-06-12 11:58:16 [INFO] - test: displays as 'test' and is type 'dummy'"

  [8]=>
  string(89) "2013-06-12 11:58:16 [INFO] - anothertest: displays as 'anothertest' and is type 'dummy'"

  [9]=>
  string(110) "2013-06-12 11:58:16 [INFO] - yetanothertest: displays as 'yetanothertestwithanothername' and is type 'dummy'"

  [10]=>
  string(60) "2013-06-12 11:58:17 [INFO] [Server] Dude, stop doing that!"
}

我想抓取项目 6 到 9,然后将它们放入一个新数组中。

为此,我们需要这样做:请注意,我在可以有任何内容的地方使用星号,只要它与星号具有相同的字符数。我在可以有任何输入的地方使用主题标签,而不取决于字符数。

  1. 使用以下语法查找数组中的最后一个条目:"****-**-** **:**:** [INFO] Showing # objective(s) on scoreboard"
  2. 使用此语法获取所有直接跟随的条目:"****-**-** **:**:** [INFO] - #: displays as '#' and is type '#'"
  3. 将它们放入数组中

我真的基于这一点。我很确定正则表达式会派上用场,但我从来没有设法理解它们。

提前致谢

**编辑:**我完全忘记了一些非常重要的事情。请阅读此评论

4

2 回答 2

2

这是一种可能的方法:

  • 创建两种模式,一种用于“显示...”消息,另一种用于“显示”消息;
  • 以相反的顺序(从结尾到开头)遍历您的数组,检查每个字符串
  • 如果字符串匹配“显示模式”,则检查每个后续字符串是否匹配“显示”模式;如果是这样,请将其放入某个容器中。匹配的字符串也应该放在这个容器中。

这是一种可能的实现:

$datePattern = '\d{4}-\d{2}-\d{2}';
$timePattern = '\d{2}:\d{2}:\d{2}';
$headerPattern = $datePattern . ' ' . $timePattern . ' \[INFO] ';
$showingPattern = $headerPattern 
    . 'Showing \d+ objective\(s\) on scoreboard';
$messagePattern = $headerPattern 
    . "- [^:]+: displays as '[^']*' and is type '[^']*'";

$results = array();

$i = $max = count($arr);
while ($i--) {
  $msg = $arr[$i];
  if (preg_match("/^$showingPattern/", $msg)) {
    $result = array($msg);
    for ($j = $i + 1; $j < $max; $j++) {
      $nextMsg = $arr[$j];
      if (preg_match("/^$messagePattern/", $nextMsg)) {
        $result[] = $nextMsg;
      }
      else {
        break;
      }
    }
    $results[$i] = $result;
  }
}
var_dump($results);

演示

于 2013-06-12T10:41:42.510 回答
2

这是raina77ow 代码的修改版本。它缺少一些检查(阅读我写的评论)

$datePattern = '\d{4}-\d{2}-\d{2}';
    $timePattern = '\d{2}:\d{2}:\d{2}';
    $headerPattern = $datePattern . ' ' . $timePattern . ' \[INFO] ';
    $showingPattern = $headerPattern 
        . 'Showing \d+ objective\(s\) on scoreboard';
    $messagePattern = $headerPattern 
        . "- [^:]+: displays as '[^']*' and is type '[^']*'";
    $noPattern = $headerPattern
        . "There are no objectives on the scoreboard";

    $results = array();

    $i = $max = count($arr);
    while ($i--) {
      $msg = $arr[$i];
      if (preg_match("/^$showingPattern/", $msg)) {
        $result = array($msg);
        for ($j = $i + 1; $j < $max; $j++) {
          $nextMsg = $arr[$j];
          if (preg_match("/^$messagePattern/", $nextMsg)) {
            $result[] = $nextMsg;
          }
          else {
            break;
          }
        }
        $results[$i] = $result;
      }
    }

    $no=preg_grep("/^$noPattern/",array_reverse($arr));

    $results=array_shift($results);
    $count=count($results);

    $notime=strtotime("the 1st of september 1971");
    $notime=strtotime(substr(array_shift($no),0,19));
    $resulttime=strtotime(substr($results[0],0,19));

    if ($resulttime>$notime) {
        for($i=0;$i<$count;$i++){
            echo substr($results[$i],27).'<br/>';
        }
    }
    else echo 'There are no objectives on the scoreboard';
于 2013-06-12T13:10:58.553 回答