PHP 排序 preg_match_all 数组

PHP Sort preg_match_all array

我正在处理一个表单处理页面,该页面获取粘贴到表单中的数据,并在提交时获取数据并将其分解为不同的匹配项。我无法找出根据特定标准对日期进行排序的编码。任何帮助都会很棒。以下是我目前为 foreach 循环编写的代码。我还包括仅包含此信息的输出内容。我希望能够按日期和其他变量排序。

下面是 运行 下面代码的输出。

694345 赞同 01/13-TR-OK 2015 年 1 月 13 日 03:32 下午 2015 年 1 月 16 日 05:00 下午 z-CHG GL 网络运营 预申请

691098 赞同 01/05-TR-PEND *不确定是否回收附件 2015 年 1 月 5 日 09:59 上午 2015 年 1 月 14 日 05:00 下午 z-CHG GL 网络运营 预申请

代码如下:

<?php

 $lines = preg_split('/\n/', $_POST['changeQueue']);     // Split Pasted info into an array of lines


foreach ($lines as $line){
if (!preg_match('/^\d\d\d\d\d\d/', $line)){ 
    continue;                   // Skip this line if it doesn't start with a 6+ digit number
};

//Regex to capture the interesting parts from each line, should work for IE and Mozilla
$pattern = '/^(\d*)\s(\d*)\s(\S*)\s(\S*)\s(.*?)\s(\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}\s\w\w)\s(\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}\s\w\w)\s(.*?)\s(.*?)\s(\w*)\s?$/';
preg_match_all($pattern,$line,$matches);

//preg_match_all returns a multidimentional array of matches, [0][x] contains the original string(s) so we start with [1][0];
$ticketNumber = $matches[1][0];
#$taskSequence = $matches[2][0];
$taskType = $matches[3][0];
#$taskStatus = $matches[4][0];
$description = $matches[5][0];
$startDate = $matches[6][0];
$endDate = $matches[7][0];
#$asignee = $matches[8][0];
$team = $matches[9][0];
$normal = $matches[10][0];


// Print the variables just to verify that we caught all the info
print_r($ticketNumber);
echo "<br>";
print_r($taskType);
echo "<br>";
print_r($description);
echo "<br>";
print_r($startDate);
echo "<br>";
print_r($endDate);
echo "<br>";
print_r($team);
echo "<br>";
print_r($normal);
echo "<hr><br><br><br>";
};
?>    
  1. 根据您的使用情况,使用 preg_split() 没有任何意义。相反,您可以轻松地使用 $lines = explode("\n", $_POST['changeQueue']);。如果您没有将正则表达式用于其他任何事情,那么这绝对是可行的方法,因此您没有启动正则表达式引擎的开销。由于您在此之后使用的是正则表达式,所以这没什么大不了的,仅供参考。

  2. 您的第一个 preg_match() 模式可以简化:'/^\d{6}/'

  3. 无论如何我不确定你是否应该使用 preg_match_all()。如果您只使用 preg_match() 并传入 $matches 参数,它将用在索引 0 处匹配的整个模式填充 $matches,但之后它将填充每个带括号的子- 它匹配的模式。所以你用一维数组而不是不必要的多维数组得到你想要的结果。

  4. 一旦你在 $matches 中有了一个一维数组,根据你的代码示例,你的日期看起来会在 $matches[6]$matches[7] 中.

现在,听起来您想按每行的开始日期或结束日期排序 $lines。因此,不是循环遍历 $lines,而是要调用 usort()。在传递给 usort() 的回调中,拆分行,获取要作为排序依据的值,并使用这些值比较两条行。然后 return 来自回调的 -101 告诉 PHP 如何对这些行进行排序。

这是一个示例(未经测试):

function parseLine($line) {
    $pattern = /* pattern to split line */;
    preg_match($pattern, $line, $matches);

    // drop the first element of the beginning of the array
    array_shift($matches);

    // we will give names to the values and return it as an associative array
    // this will help with accessing these values later on
    return array_combine(
        array(
            'ticketNumber',
            'taskSequence',
            'taskType',
            'taskStatus',
            'description',
            'startDate',
            'endDate',
            'asignee',
            'team',
            'normal',
        ),
        array_slice($matches, 0, 10)
    );
}

function compareLines($line1, $line2) {
    if ($line1['startDate'] < $line1['startDate']) {
        return -1;
    } else if ($line1['startDate'] == $line1['startDate']) {
        return 0;
    } else {
        return 1;
    }
}

$lines = explode("\n", $_POST['changeQueue']);
$parsedLines = array();
foreach ($lines as $line) {
    if (!preg_match('/^\d{6}/', $line)) {
        continue;
    }
    $parsedLines[] = parseLine($line);
}
usort($parsedLines, 'compareLines');

var_dump($parsedLines);