2013-08-31 3 views
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]; 

여기서는 첫 번째 단어 만 출력하고 있습니다.

누구든지 나를 데리고와주세요. 감사.

+0

$ 키 파이프 분리 문자열이 배열 –

+0

그래이어야한다. 또는'implode ('|', $ key)'. – Jason

+0

@Dagon and jason 답장을 보내 주셔서 감사합니다. 내가 잘못하지 않았다면 배열을 사용할 수 있기를 바랍니다. 위의 코드는 특정 단어 뒤에 단어를 찾는 것입니다. 하지만 지금은 데이트를 찾고 싶습니다. – newcomer

답변

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 
     ) 

) 
+0

awsome, 도움 주셔서 감사합니다. 나는 d/m/y와 같은 형식으로 모든 결과를 얻길 원합니다. 동일한 형식으로 모든 날짜를 변환 할 수있는 옵션이 있습니다. – newcomer

관련 문제