正则表达式,用于匹配纬度和经度函数

我的文件上有纬度和经度数据,我正在尝试通过convertor函数调用该数据。


我试图为该$result60行添加功能,但它不起作用。我正在尝试传递该函数的值,因此它将使用DM方法计算正确的纬度和经度。


试图

$re60 = '/([EW])([0-9][0-9][0-9])([0-9][0-9])/s';

$str60 = 'E16130';

//$subst60 = '\\3\\2\\1';

$subst60 = DMS2Decimal($degr = \\2, $mins = \\3, $secs = 0, $dir = \\1);

$result60 = preg_replace($re60, $subst60, $str60);

echo "The result of the substitution is ".$result60;

我该如何解决这个问题?


梵蒂冈之花
浏览 184回答 1
1回答

慕盖茨4494581

您可以使用此RegEx将输入字符串分成3个组,其中group为组$1,$2并且$3可以由DMS方法调用return&nbsp;$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:&nbsp; y: " . $result60;function DMS2Decimal($degrees = 0, $minutes = 0, $seconds = 0, $direction = 'n'){&nbsp; &nbsp; //converts DMS coordinates to decimal&nbsp; &nbsp; //returns false on bad inputs, decimal on success&nbsp; &nbsp; //direction must be n, s, e or w, case-insensitive&nbsp; &nbsp; $d = strtolower($direction);&nbsp; &nbsp; $ok = array('n', 's', 'e', 'w');&nbsp; &nbsp; //degrees must be integer between 0 and 180&nbsp; &nbsp; if (!is_numeric($degrees) || $degrees < 0 || $degrees > 180) {&nbsp; &nbsp; &nbsp; &nbsp; $decimal = false;&nbsp; &nbsp; }&nbsp; &nbsp; //minutes must be integer or float between 0 and 59&nbsp; &nbsp; elseif (!is_numeric($minutes) || $minutes < 0 || $minutes > 59) {&nbsp; &nbsp; &nbsp; &nbsp; $decimal = false;&nbsp; &nbsp; }&nbsp; &nbsp; //seconds must be integer or float between 0 and 59&nbsp; &nbsp; elseif (!is_numeric($seconds) || $seconds < 0 || $seconds > 59) {&nbsp; &nbsp; &nbsp; &nbsp; $decimal = false;&nbsp; &nbsp; } elseif (!in_array($d, $ok)) {&nbsp; &nbsp; &nbsp; &nbsp; $decimal = false;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; //inputs clean, calculate&nbsp; &nbsp; &nbsp; &nbsp; $decimal = $degrees + ($minutes / 60) + ($seconds / 3600);&nbsp; &nbsp; &nbsp; &nbsp; //reverse for south or west coordinates; north is assumed&nbsp; &nbsp; &nbsp; &nbsp; if ($d == 's' || $d == 'w') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $decimal *= -1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $decimal;}输出The result of the substitution:&nbsp; y: 161.5027777777
打开App,查看更多内容
随时随地看视频慕课网APP