php以十进制格式验证纬度/经度字符串

时间:2022-03-08 16:31:01

Alright I have what I would call a massive list of longitude and latitude coordinates. That said I also have a handful of sources I pull these coordinates in from. Some of them come from get/post methods which can cause potential security holes in my site/service. So I am trying to figure out how to validate longitude and latitude via PHP. I was thinking something regex via preg_match. But I could be wrong, maybe there's an easier way someone would like to suggest. I've tried my own concepts, and I have tried various internet brew concepts of trying to find a regex pattern that will validate these for me via preg_match (or similar again if you got a better suggestion I am all ears).

好了,我有了一个巨大的经纬度坐标列表。也就是说,我也有一些资源我从这些坐标中提取出来。其中一些来自get/post方法,可能会在我的站点/服务中造成潜在的安全漏洞。所以我正在尝试通过PHP来验证经度和纬度。我在想什么regex通过preg_match。但我可能是错的,也许有一个更简单的方法,有人会建议。我已经尝试了我自己的概念,并且尝试了各种internet brew概念,试图找到一个regex模式,通过preg_match为我验证这些概念(或者如果您有更好的建议,我洗耳恭听)。

My Last failed attempt prior to finally caving in and coming here was..

我最后一次失败的尝试是…

preg_match('^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$', $geoResults['latitude']))

which yields " preg_match() [function.preg-match]: No ending delimiter '^' found " as my error. Last couple attempts I have tried yielded that error so I have no idea what it is or means.

它产生“preg_match()”[函数。preg-match]:没有结束分隔符“^”发现“我的错误。我试过的最后几次尝试都失败了,所以我不知道这是什么意思。

5 个解决方案

#1


22  

Add forward slashes to the beginning and end of the match sequence to make it valid regex syntax:

将前斜杠添加到匹配序列的开头和结尾,使其成为有效的regex语法:

preg_match('/^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/', $geoResults['latitude']);

For your question on whether to use regular expressions (regex) or not, in this case using regex (PCRE preg_match()) is the best way to secure your site. When matching variable complex string arrangements, regex is the way to go. It's common for developers to turn to regex for matching a static string such as 'abc'. This is when strpos() or str_replace() are better choices.

关于是否使用正则表达式(regex)的问题,在本例中,使用regex (PCRE preg_match())是保护站点安全的最佳方式。当匹配复杂的变量字符串安排时,应该使用regex。对于开发人员来说,使用regex匹配静态字符串(如“abc”)是很常见的。这时strpos()或str_replace()是更好的选择。

#2


20  

It's a bit old question, but anyway I post my solution here:

这是个老问题,但不管怎样,我在这里发表了我的解决方案:

preg_match('/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?);[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/', $geoResults['latlng']);

I assumed here that u split lat. from lng. by semicolon. If u want to check only lat. or only lng. here are regexp's;

这里假设u分裂了lat。从液化天然气。分号。如果你只想检查lat。或者只有液化天然气。下面是正则表达式的;

Rgx for lat.:

Rgx lat。:

/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/

Rgx for lng.:

Rgx为液化天然气。

/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/

Here is an improved online demo: https://regex101.com/r/bV5fA1/1

这里有一个改进的在线演示:https://regex101.com/r/bV5fA1/1

#3


1  

Why not use modern and unit tested Assertion library for that?

为什么不使用现代的和单元测试的断言库呢?

Example Value Object class:

示例值对象类:

<?php
namespace MyApp\ValueObject;

use Assert\Assertion;

class Location
{
    private $lat;
    private $lon;

    public function __construct($lat, $lon)
    {
        Assertion::greaterOrEqualThan($lat, -90.0);
        Assertion::lessOrEqualThan($lat, 90.0);
        Assertion::greaterOrEqualThan($lon, -180.0);
        Assertion::lessOrEqualThan($lon, 180.0);

        $this->lat = $lat;
        $this->lon = $lon;
    }

    public function latitude()
    {
        return $this->lat;
    }

    public function longitude()
    {
        return $this->lon;
    }

    public function __toString()
    {
        return $this->lat . ' ' . $this->lon;
    }

Usage:

用法:

$location = new \MyApp\ValueObject\Location(24.7, -20.4059);

echo $location->latitude() , ' ' , $location->longitude();

// or simply

echo $location;

#4


0  

I want to validate latitude and longitude, too, and have this result:

我也想验证纬度和经度,得到以下结果:

-90.0000 - 90.0000

-90.0000 - 90.0000

^-?([0-9]|[1-8][0-9]|90)\.{1}\d{4}$

-180.0000 - 180.0000

-180.0000 - 180.0000

^-?([0-9]|[1-9][0-9]|1[0-7][0-9]|180)\.{1}\d{4}$

I have tested here with pcre.

我在这里用pcre测试过。

#5


0  

It's real work unicum solution in net " -90.0000 - 90.0000

这是一个真正的单孔解决方案在净" -90.0000 -90.0000

^-?([0-9]|[1-8][0-9]|90)\.{1}\d{4}$

-180.0000 - 180.0000

^-?([0-9]|[1-9][0-9]|1[0-7][0-9]|180)\.{1}\d{4}$
"

For &lat=90.000000 &lon=180.000000 :

lat = 90.000000经度= 180.000000:

"/^-?([0-9]|[1-8][0-9]|90)\.{1}\d{1,6}$/"

"/^-?([1]?[1-7][1-9]|[1]?[1-8][0]|[1-9]?[0-9])\.{1}\d{1,6}/"

#1


22  

Add forward slashes to the beginning and end of the match sequence to make it valid regex syntax:

将前斜杠添加到匹配序列的开头和结尾,使其成为有效的regex语法:

preg_match('/^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/', $geoResults['latitude']);

For your question on whether to use regular expressions (regex) or not, in this case using regex (PCRE preg_match()) is the best way to secure your site. When matching variable complex string arrangements, regex is the way to go. It's common for developers to turn to regex for matching a static string such as 'abc'. This is when strpos() or str_replace() are better choices.

关于是否使用正则表达式(regex)的问题,在本例中,使用regex (PCRE preg_match())是保护站点安全的最佳方式。当匹配复杂的变量字符串安排时,应该使用regex。对于开发人员来说,使用regex匹配静态字符串(如“abc”)是很常见的。这时strpos()或str_replace()是更好的选择。

#2


20  

It's a bit old question, but anyway I post my solution here:

这是个老问题,但不管怎样,我在这里发表了我的解决方案:

preg_match('/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?);[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/', $geoResults['latlng']);

I assumed here that u split lat. from lng. by semicolon. If u want to check only lat. or only lng. here are regexp's;

这里假设u分裂了lat。从液化天然气。分号。如果你只想检查lat。或者只有液化天然气。下面是正则表达式的;

Rgx for lat.:

Rgx lat。:

/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/

Rgx for lng.:

Rgx为液化天然气。

/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/

Here is an improved online demo: https://regex101.com/r/bV5fA1/1

这里有一个改进的在线演示:https://regex101.com/r/bV5fA1/1

#3


1  

Why not use modern and unit tested Assertion library for that?

为什么不使用现代的和单元测试的断言库呢?

Example Value Object class:

示例值对象类:

<?php
namespace MyApp\ValueObject;

use Assert\Assertion;

class Location
{
    private $lat;
    private $lon;

    public function __construct($lat, $lon)
    {
        Assertion::greaterOrEqualThan($lat, -90.0);
        Assertion::lessOrEqualThan($lat, 90.0);
        Assertion::greaterOrEqualThan($lon, -180.0);
        Assertion::lessOrEqualThan($lon, 180.0);

        $this->lat = $lat;
        $this->lon = $lon;
    }

    public function latitude()
    {
        return $this->lat;
    }

    public function longitude()
    {
        return $this->lon;
    }

    public function __toString()
    {
        return $this->lat . ' ' . $this->lon;
    }

Usage:

用法:

$location = new \MyApp\ValueObject\Location(24.7, -20.4059);

echo $location->latitude() , ' ' , $location->longitude();

// or simply

echo $location;

#4


0  

I want to validate latitude and longitude, too, and have this result:

我也想验证纬度和经度,得到以下结果:

-90.0000 - 90.0000

-90.0000 - 90.0000

^-?([0-9]|[1-8][0-9]|90)\.{1}\d{4}$

-180.0000 - 180.0000

-180.0000 - 180.0000

^-?([0-9]|[1-9][0-9]|1[0-7][0-9]|180)\.{1}\d{4}$

I have tested here with pcre.

我在这里用pcre测试过。

#5


0  

It's real work unicum solution in net " -90.0000 - 90.0000

这是一个真正的单孔解决方案在净" -90.0000 -90.0000

^-?([0-9]|[1-8][0-9]|90)\.{1}\d{4}$

-180.0000 - 180.0000

^-?([0-9]|[1-9][0-9]|1[0-7][0-9]|180)\.{1}\d{4}$
"

For &lat=90.000000 &lon=180.000000 :

lat = 90.000000经度= 180.000000:

"/^-?([0-9]|[1-8][0-9]|90)\.{1}\d{1,6}$/"

"/^-?([1]?[1-7][1-9]|[1]?[1-8][0]|[1-9]?[0-9])\.{1}\d{1,6}/"