为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
梵蒂冈之花 2021-04-27 13:50:33
我的文件上有纬度和经度数据,我正在尝试通过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;我该如何解决这个问题?
查看完整描述

1 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

您可以使用此RegEx将输入字符串分成3个组,其中group为组$1$2并且$3可以由DMS方法调用return $decimal

正则表达式

/([EWSN])([0-9]{3})([0-9]{2})/s

//img1.sycdn.imooc.com//609504de0001966a05320208.jpg

代码

$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


查看完整回答
反对 回复 2021-05-07
  • 1 回答
  • 0 关注
  • 181 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信