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 に答える