从 1 个字符串中提取不同模式的 lat long date time float 并将其拆分为变量
Extract different patterns of lat long date time float from 1 string and split it to variables
我需要一个可以将这个单个字符串变量拆分为多个变量的正则表达式专家。来自源的数据有换行符但不显示 \n。
我会很感激的。
$text_utf8_test = "lat:24.910717 lon:67.073710
speed:0.00
T:17/02/22 17:00
http://maps.google.com/maps?f=q&q=24.910717,67.073710&z=16
Pwr: ON Door: OFF ACC: OFF";
拆分后预期 PHP 个变量
echo $lat;
echo $long;
echo $speed;
echo $time;
echo $pwr;
echo $door;
echo $acc;
预期输出结果
24.910717 /*This value can be vary due to dynamic update from location*/
67.073710
0.00 /*this can be from zero to 320*/
ON /*this can be ON or OFF*/
OFF /*this can be ON or OFF*/
OFF /*this can be ON or OFF*/
$text_utf8_test = "lat:24.910717 lon:67.073710
speed:0.00
T:17/02/22 17:59
http://maps.google.com/maps?f=q&q=24.910717,67.073710&z=16
Pwr: ON Door: OFF ACC: OFF";
$phone_test = "88888888888";
echo $text_utf8_test;
preg_match('/([0-9.-]+).+?([0-9.-]+)/', $text_utf8_test, $matches); /*For lat long exclusively*/
//preg_match('/OFF/', $text_utf8_test, $test);
$keywords = preg_split("/[\s,]+/", $text_utf8_test); /*For spliting this whole string*/
preg_match('~speed:([-+]?[0-9]*\.?[0-9]*)~', $keywords[2], $speed ); /*For grabing speed*/
preg_match('/(\d{2})\/(\d{2})\/(\d{2})(\d{2}):(\d{2})$/', $keywords[3].$keywords[4], $split_date_time ); /*For grabing date time*/
/*Spliting and combining date time into sql format*/
if(!empty($split_date_time[3])){
$date = $split_date_time[3].$split_date_time[2].$split_date_time[1];
echo "</br>";
$date_sanitized1 = strtotime($split_date_time[2]."/".$split_date_time[3]."/".$split_date_time[1]);
$date_sanitized2 = date('Y-m-d',$date_sanitized1);
$date_time = $date_sanitized2." ".$split_date_time[4].":".$split_date_time[5].":"."00.000" ;
}
if((!empty($keywords[2])) && (!empty($keywords[8])) && (!empty($keywords[10])) && (!empty($keywords[12]))){
$speed=$speed[1];
$power=$keywords[8];
$door=$keywords[10];
$acc=$keywords[12];
}
$lat=$matches[1];
$long=$matches[2];
if ((!empty($lat)) && (!empty($long)) && (!empty($speed)) && (!empty($date_time)) && (!empty($power)) && (!empty($door)) && (!empty($acc)) ){
echo "</br>";
echo $lat;
echo "</br>";
echo $long;
echo "</br>";
echo $speed;
echo "</br>";
echo $date_time;
echo "</br>";
echo $power;
echo "</br>";
echo $door;
echo "</br>";
echo $acc;
echo "</br>";
}else{
echo "noting";
}
输出
24.910717
67.073710
0.00
2017-02-22 17:59:00.000
ON
OFF
OFF
我需要一个可以将这个单个字符串变量拆分为多个变量的正则表达式专家。来自源的数据有换行符但不显示 \n。 我会很感激的。
$text_utf8_test = "lat:24.910717 lon:67.073710
speed:0.00
T:17/02/22 17:00
http://maps.google.com/maps?f=q&q=24.910717,67.073710&z=16
Pwr: ON Door: OFF ACC: OFF";
拆分后预期 PHP 个变量
echo $lat;
echo $long;
echo $speed;
echo $time;
echo $pwr;
echo $door;
echo $acc;
预期输出结果
24.910717 /*This value can be vary due to dynamic update from location*/
67.073710
0.00 /*this can be from zero to 320*/
ON /*this can be ON or OFF*/
OFF /*this can be ON or OFF*/
OFF /*this can be ON or OFF*/
$text_utf8_test = "lat:24.910717 lon:67.073710
speed:0.00
T:17/02/22 17:59
http://maps.google.com/maps?f=q&q=24.910717,67.073710&z=16
Pwr: ON Door: OFF ACC: OFF";
$phone_test = "88888888888";
echo $text_utf8_test;
preg_match('/([0-9.-]+).+?([0-9.-]+)/', $text_utf8_test, $matches); /*For lat long exclusively*/
//preg_match('/OFF/', $text_utf8_test, $test);
$keywords = preg_split("/[\s,]+/", $text_utf8_test); /*For spliting this whole string*/
preg_match('~speed:([-+]?[0-9]*\.?[0-9]*)~', $keywords[2], $speed ); /*For grabing speed*/
preg_match('/(\d{2})\/(\d{2})\/(\d{2})(\d{2}):(\d{2})$/', $keywords[3].$keywords[4], $split_date_time ); /*For grabing date time*/
/*Spliting and combining date time into sql format*/
if(!empty($split_date_time[3])){
$date = $split_date_time[3].$split_date_time[2].$split_date_time[1];
echo "</br>";
$date_sanitized1 = strtotime($split_date_time[2]."/".$split_date_time[3]."/".$split_date_time[1]);
$date_sanitized2 = date('Y-m-d',$date_sanitized1);
$date_time = $date_sanitized2." ".$split_date_time[4].":".$split_date_time[5].":"."00.000" ;
}
if((!empty($keywords[2])) && (!empty($keywords[8])) && (!empty($keywords[10])) && (!empty($keywords[12]))){
$speed=$speed[1];
$power=$keywords[8];
$door=$keywords[10];
$acc=$keywords[12];
}
$lat=$matches[1];
$long=$matches[2];
if ((!empty($lat)) && (!empty($long)) && (!empty($speed)) && (!empty($date_time)) && (!empty($power)) && (!empty($door)) && (!empty($acc)) ){
echo "</br>";
echo $lat;
echo "</br>";
echo $long;
echo "</br>";
echo $speed;
echo "</br>";
echo $date_time;
echo "</br>";
echo $power;
echo "</br>";
echo $door;
echo "</br>";
echo $acc;
echo "</br>";
}else{
echo "noting";
}
输出
24.910717
67.073710
0.00
2017-02-22 17:59:00.000
ON
OFF
OFF