如何将带有日期的字符串拆分为分隔符?

时间:2013-06-04 09:54:25

标签: php mysql

你能帮我把大字符串分成子串吗?

方案: 在MySQL表中的一个字段中,我有一个字符串,其中有一个活动日志,格式如下:

yyyy-mm-dd hh:mm:ss - Name1 Surname1 - Activity1
yyyy-mm-dd hh:mm:ss - Name2 Surname2 - Activity2
yyyy-mm-dd hh:mm:ss - Name3 Surname3 - Multiline Activity1
Multiline Activity2
Multiline Activity3
yyyy-mm-dd hh:mm:ss - Name4 Surname4 - Activity4

问题:

如何将上述数据拆分为数组:

id       date                    name                    activity
1        yyyy-mm-dd hh:mm:ss     Name1 Surname1          Activity1
2        yyyy-mm-dd hh:mm:ss     Name2 Surname2          Activity2
3        yyyy-mm-dd hh:mm:ss     Name3 Surname3          Multiline Activity 1, Multiline Activity2, Multiline Activity 3
4        yyyy-mm-dd hh:mm:ss     Name4 Surname4          Activity4

使用PHP或MySQL查询?

我知道它可以以某种方式完成,但不幸的是,由于PHP / MySQL的知识不允许我用变量分隔符(日期)解析文本,或者可能还有另一种方式?

2 个答案:

答案 0 :(得分:1)

您可以尝试 preg_split

$string = <<<STR
2013-06-04 12:12:12 - Name1 Surname1 - Activity1
2013-06-04 12:12:12 - Name2 Surname2 - Activity2
2013-06-04 12:12:12 - Name3 Surname3 - Multiline Activity1
Multiline Activity2
Multiline Activity3
2013-06-04 12:12:12 - Name4 Surname4 - Activity4
STR;

$arr = preg_split("/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) - (.*) - (.*)[\r\n]?/", $string, null, PREG_SPLIT_DELIM_CAPTURE);

$arr = array_filter($arr);

print_r($arr);

输出

Array
(
    [1] => 2013-06-04 12:12:12
    [2] => Name1 Surname1
    [3] => Activity1
    [5] => 2013-06-04 12:12:12
    [6] => Name2 Surname2
    [7] => Activity2
    [9] => 2013-06-04 12:12:12
    [10] => Name3 Surname3
    [11] => Multiline Activity1
    [12] => Multiline Activity2
Multiline Activity3

    [13] => 2013-06-04 12:12:12
    [14] => Name4 Surname4
    [15] => Activity4
)

实际使用示例:

$i = -1; 
$result = array();

foreach($arr as $value) {
    if(preg_match("/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/", $value)) {
        // 1st element - datetime
        $i++;
        $result[$i]['datetime'] = $value;
    } elseif(!isset($result[$i]['name'])) {
        // 2nd element - name
        $result[$i]['name'] = $value;
    } else {
        // 3rd element - activities separated by comma
        if(!isset($result[$i]['activities'])) {
            $result[$i]['activities'] = $value;
        } else {
            $result[$i]['activities'] .= ', ' . $value;        
        }
    }
}

print_r($result);

<强>输出:

Array
(
    [0] => Array
        (
            [datetime] => 2013-06-04 12:12:12
            [name] => Name1 Surname1
            [activities] => Activity1
        )

    [1] => Array
        (
            [datetime] => 2013-06-04 12:12:12
            [name] => Name2 Surname2
            [activities] => Activity2
        )

    [2] => Array
        (
            [datetime] => 2013-06-04 12:12:12
            [name] => Name3 Surname3
            [activities] => Multiline Activity1
, Multiline Activity2
Multiline Activity3

        )

    [3] => Array
        (
            [datetime] => 2013-06-04 12:12:12
            [name] => Name4 Surname4
            [activities] => Activity4
        )

)

答案 1 :(得分:0)

循环遍历MySQL结果并为每一行执行以下操作:

$row = explode(' - ', $row);