1

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

$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];

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

请任何人指导我获取日期。谢谢。

4

1 回答 1

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
        )

)
于 2013-08-31T12:24:14.457 回答