how can i get Arrival Date and Departure Date from string?
Guest Name :WOLAK, KAMIL - Arrival Date: 2022-09-29 - Departure Date: 2022-10-06 - Ref: H242806
using php
CodePudding user response:
You may use a regular expressions to match the format of yyyy-mm-dd:
$re = '/(\d\d\d\d-\d\d-\d\d)/m';
$str = 'WOLAK, KAMIL - Arrival Date: 2022-09-29 - Departure Date: 2022-10-06 - Ref: H242806';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
output:
Array
(
[0] => Array
(
[0] => 2022-09-29
[1] => 2022-09-29
)
[1] => Array
(
[0] => 2022-10-06
[1] => 2022-10-06
)
)
CodePudding user response:
$str = "WOLAK, KAMIL - Arrival Date: 2022-09-29 - Departure Date: 2022-10-
06 - Ref: H242806";
$data = explode("- ",$str);
$new_data = [];
foreach($data as $key => $value){
if(str_contains($value,"Arrival Date:")){
$date = explode(":", $value);
$new_data["arrival_date"] = $date[1];
}else if(str_contains($value,"Departure Date:")){
$date = explode(":", $value);
$new_data["departure_date"] = $date[1];
}
}
print_r($new_data);
you will get the result as
Array ( [arrival_date] => 2022-09-29 [departure_date] => 2022-10-06 )