查找特定单词后的日期

时间:2013-08-31 03:34:17

标签: php preg-match-all

我想在特定单词(键)之后的字符串中找到日期。 我的字符串是动态的,从一个字符串到另一个字符串的日期格式也不相同。

$data = "Balance- 0.30,Val-Aug 29 2013, Free Bal- 0.00";
or
$data = "Bal: 96.27.Valid Sep 26 2013.Toll Free Dial 578785";
or
$data = "BalanceRs.0.00,Expiry date: Apr 04 20141 Live Scores";
or
$data = "Your current balance is 0.20.Your account expires on 2013-11-23 23:59:59.";
or
$data = "Main Bal Rs.87.850 Val 09-07-2014,More";


$key = array('Val-','Val','Valid','Expiry date:','expires on');

$result=preg_match_all("/(?<=(".$key."))(\s\w*)/i",$data,$networkID);
$myanswer = @$networkID[0][0];

这里我只得到第一个单词的输出。

任何人都可以指导我去约会。感谢。

1 个答案:

答案 0 :(得分:2)

怎么样:

$data = "Balance- 0.30,Val-Aug 29 2013, Free Bal- 0.00";
$data .= "Bal: 96.27.Valid Sep 26 2013.Toll Free Dial 578785";
$data .= "BalanceRs.0.00,Expiry date: Apr 04 20141 Live Scores";
$data .= "Your current balance is 0.20.Your account expires on 2013-11-23 23:59:59.";
$data .= "Main Bal Rs.87.850 Val 09-07-2014,More";

$key = array('Valid','Val-','Val','Expiry date:','expires on');
$key_str = implode('|', $key);
preg_match_all("/(?<=$key_str)\s*((?:\w{3} \d\d \d{4})|(?:\d{4}-\d\d-\d\d)|(?:\d\d-\d\d-\d{4}))/i", $data, $networkID);
print_r($networkID);

<强>输出:

Array
(
    [0] => Array
        (
            [0] => Aug 29 2013
            [1] =>  Sep 26 2013
            [2] =>  Apr 04 2014
            [3] =>  2013-11-23
            [4] =>  09-07-2014
        )

    [1] => Array
        (
            [0] => Aug 29 2013
            [1] => Sep 26 2013
            [2] => Apr 04 2014
            [3] => 2013-11-23
            [4] => 09-07-2014
        )

)