Php 获取包含某个字符串的所有数组项,该字符串彼此相邻,且第一行之前的行
我有一个PHP数组,看起来像这样:Php 获取包含某个字符串的所有数组项,该字符串彼此相邻,且第一行之前的行,php,arrays,text-search,Php,Arrays,Text Search,我有一个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
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!"
}
数组(10){
[1]=>
字符串(69)“2013-06-12 11:25:44[信息]记分牌上没有目标”
[2]=>
string(53)“2013-06-12 11:42:27[信息][服务器]嗨,你好吗?”
[3]=>
字符串(86)“2013-06-12 11:43:40[信息]用法:/scoreboard目标”
[4]=>
字符串(95)“2013-06-12 11:57:51[信息]/记分板目标添加[显示名称…””
[5]=>
字符串(67)“2013-06-12 11:57:59[信息]成功添加了新目标“测试”
[6]=>
字符串(64)“2013-06-12 11:58:16[信息]在记分板上显示3个目标”
[7]=>
字符串(74)“2013-06-12 11:58:16[INFO]-测试:显示为“测试”,类型为“虚拟”
[8]=>
字符串(89)“2013-06-12 11:58:16[INFO]-另一个测试:显示为'anothertest',类型为'dummy'”
[9]=>
字符串(110)“2013-06-12 11:58:16[INFO]-yetanothertest:显示为'yetanothertestwithanothername',类型为'dummy'”
[10]=>
字符串(60)“2013-06-12 11:58:17[信息][服务器]伙计,别这样!”
}
我想抓取第六到第九项,然后将它们放入一个新数组
为此,我们需要这样做:
请注意,我在可以有任何内容的地方使用星号,只要它与星号具有相同的字符数。
我在可以有任何输入的地方使用hashtag,这不取决于字符数
“*****-***-*****:***:***:***[INFO]在记分板上显示#目标”
“*****-***-***:***:***:***:***[INFO]-#:显示为“#”,类型为“#”。
**编辑:*我完全忘记了一些非常重要的事情。请阅读。这里有一种可能的方法:
- 创建两个模式,一个用于“显示…”消息,另一个用于“显示”消息李>
- 以相反的顺序(从末尾到开头)遍历数组,检查每个字符串
- 如果字符串与“显示模式”匹配,则检查每个后续字符串是否与“显示”模式匹配;如果有的话,把它放进一些容器里。匹配的字符串可能也应该放在这个容器中
$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);
$datePattern='\d{4}-\d{2}-\d{2}';
$timePattern='\d{2}:\d{2}:\d{2}';
$headerPattern=$datePattern.'''$时间模式。”\[资讯]';
$showingPattern=$headerPattern
. '在记分板上显示\d+目标\(s\);
$messagePattern=$headerPattern
. “-[^:::+:显示为“[^']*”,类型为“[^']*”;
$results=array();
$i=$max=计数($arr);
而($i--){
$msg=$arr[$i];
if(preg_match(“/^$showingPattern/”,$msg)){
$result=数组($msg);
对于($j=$i+1;$j<$max;$j++){
$nextsg=$arr[$j];
if(preg_match(“/^$messagePattern/”,$nextMsg)){
$result[]=$nextsg;
}
否则{
打破
}
}
$results[$i]=$result;
}
}
var_dump($结果);
.这里有一种可能的方法:
- 创建两个模式,一个用于“显示…”消息,另一个用于“显示”消息李>
- 以相反的顺序(从末尾到开头)遍历数组,检查每个字符串
- 如果字符串与“显示模式”匹配,则检查每个后续字符串是否与“显示”模式匹配;如果有的话,把它放进一些容器里。匹配的字符串可能也应该放在这个容器中
$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);
$datePattern='\d{4}-\d{2}-\d{2}';
$timePattern='\d{2}:\d{2}:\d{2}';
$headerPattern=$datePattern.'''$时间模式。”\[资讯]';
$showingPattern=$headerPattern
. '在记分板上显示\d+目标\(s\);
$messagePattern=$headerPattern
. “-[^:::+:显示为“[^']*”,类型为“[^']*”;
$results=array();
$i=$max=计数($arr);
而($i--){
$msg=$arr[$i];
if(preg_match(“/^$showingPattern/”,$msg)){
$result=数组($msg);
对于($j=$i+1;$j<$max;$j++){
$nextsg=$arr[$j];
if(preg_match(“/^$messagePattern/”,$nextMsg)){
$result[]=$nextsg;
}
否则{
打破
}
}
$results[$i]=$result;
}
}
var_dump($结果);
.这是raina77ow代码的修改版本。它缺少一些检查(阅读我写的评论)
$datePattern='\d{4}-\d{2}-\d{2}';
$timePattern='\d{2}:\d{2}:\d{2}';
$headerPattern=$datePattern.'''$时间模式。”\[资讯]';
$showingPattern=$headerPattern
. '在记分板上显示\d+目标\(s\);
$messagePattern=$headerPattern
. “-[^:::+:显示为“[^']*”,类型为“[^']*”;
$noPattern=$headerPattern
. “记分牌上没有目标”;
$results=array();
$i=$max=计数($arr);
而($i--){
$msg=$arr[$i];
if(preg_match(“/^$showingPattern/”,$msg)){
$result=数组($msg);
对于($j=$i+1;$j<$max;$j++){
$nextsg=$arr[$j];
if(preg_match(“/^$messagePattern/”,$nextMsg)){
$result[]=$nextsg;
}
否则{
打破
}
}
$results[$i]=$result;
}
}
$no=preg_grep(“/^$noPattern/”,数组_reverse($arr));
$results=数组移位($results);
$count=计数($results);
$notime=标准时间(“1971年9月1日”);
$notime=strotime(substr(数组移位($no),0,19));
$resulttime=strottime(substr($results[0],0,19));
如果($resulttime>$notime){
对于($i=0;$i这是raina77ow代码的修改版本。它缺少一些检查(请阅读我写的评论)
$datePa