1 回答
TA贡献1850条经验 获得超11个赞
您可以使用此RegEx将输入字符串分成3个组,其中group为组$1
,$2
并且$3
可以由DMS方法调用return $decimal
。
正则表达式
/([EWSN])([0-9]{3})([0-9]{2})/s
代码
$str60 = 'E16130';
preg_match_all('/([EWSN])([0-9]{3})([0-9]{2})/s', $str60, $matches);
$result60 = DMS2Decimal($degrees = (int) $matches[2][0], $minutes = (int) $matches[3][0], $seconds = 10, $direction = strtolower($matches[1][0]));
echo "The result of the substitution: y: " . $result60;
function DMS2Decimal($degrees = 0, $minutes = 0, $seconds = 0, $direction = 'n')
{
//converts DMS coordinates to decimal
//returns false on bad inputs, decimal on success
//direction must be n, s, e or w, case-insensitive
$d = strtolower($direction);
$ok = array('n', 's', 'e', 'w');
//degrees must be integer between 0 and 180
if (!is_numeric($degrees) || $degrees < 0 || $degrees > 180) {
$decimal = false;
}
//minutes must be integer or float between 0 and 59
elseif (!is_numeric($minutes) || $minutes < 0 || $minutes > 59) {
$decimal = false;
}
//seconds must be integer or float between 0 and 59
elseif (!is_numeric($seconds) || $seconds < 0 || $seconds > 59) {
$decimal = false;
} elseif (!in_array($d, $ok)) {
$decimal = false;
} else {
//inputs clean, calculate
$decimal = $degrees + ($minutes / 60) + ($seconds / 3600);
//reverse for south or west coordinates; north is assumed
if ($d == 's' || $d == 'w') {
$decimal *= -1;
}
}
return $decimal;
}
输出
The result of the substitution: y: 161.5027777777
- 1 回答
- 0 关注
- 181 浏览
添加回答
举报