如何确定PHP字符串是否仅包含纬度和经度

时间:2022-02-19 16:04:09

I have to work with strings which may contain Lat/Long data, like this:

我必须处理可能包含Lat / Long数据的字符串,如下所示:

$query = "-33.805789,151.002060";
$query = "-33.805789, 151.002060";
$query = "OVER HERE: -33.805789,151.002060";

For my purposes, the first 2 strings are correct, but the last one isn't. I am trying to figure out a match pattern which would match a lat and long separated by a comma, or a comma and a space. But if it has anything in the string other than numbers, spaces, dots, minus signs and commas, then it should fail the match.

就我的目的而言,前2个字符串是正确的,但最后一个字符串不正确。我试图找出一个匹配模式,匹配lat和long用逗号,逗号和空格分隔。但是如果除了数字,空格,圆点,减号和逗号之外它在字符串中有任何东西,那么它应该使匹配失败。

Hope this makes sense, and TIA!

希望这有道理,TIA!

4 个解决方案

#1


10  

^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$

The ^ at the start and $ at the end make sure that it matches the complete string, and not just a part of it.

开头的^和结尾的$确保它匹配完整的字符串,而不仅仅是它的一部分。

#2


1  

It's simplest to solve with a regex as suggested in the other answers. Here is a step-by-step approach that would work too:

如其他答案所示,使用正则表达式解决它是最简单的。这是一个循序渐进的方法,也可以工作:

$result = explode(",", $query);  // Split the string by commas
$lat = trim($result[0]);         // Clean whitespace
$lon = trim($result[1]);         // Clean whitespace

if ((is_numeric($lat)) and (is_numeric($lon))) echo "Valid coordinates!";

This solution will accept arbitrary data after a comma:

此解决方案将在逗号后接受任意数据:

 "-33.805789,151.002060,ABNSBOFVJDPENVÜE";

will pass as ok.

会过得好。

As Frank Farmer correctly notes, is_numeric will also recognize scientific notation.

正如Frank Farmer正确指出的那样,is_numeric也将识别科学记数法。

#3


0  

/^-*\d*\.\d+,[\b]*-*\d*\.\d+$/

#4


0  

The regex approach can't really validate that longitude and latitude are valid, but here's one that would be more precise than the others posted already:

正则表达式方法无法真正验证经度和纬度是否有效,但这里的方法比已经发布的其他方法更精确:

/^\s*-?\d{1,3}\.\d+,\s*\d{1,3}\.\d+\s*$/

This would reject some strings that others' solutions would allow, such as

这会拒绝其他人的解决方案允许的一些字符串,例如

-1-23-1-,210-
--123.1234,123.1234

But it would still allow invalid values like this:

但它仍然会允许这样的无效值:

361.1234, 123.1234

Your best bet -- if you need serious validation -- is to create a class to store and validate these coordinates.

您最好的选择 - 如果您需要认真验证 - 是创建一个类来存储和验证这些坐标。

#1


10  

^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$

The ^ at the start and $ at the end make sure that it matches the complete string, and not just a part of it.

开头的^和结尾的$确保它匹配完整的字符串,而不仅仅是它的一部分。

#2


1  

It's simplest to solve with a regex as suggested in the other answers. Here is a step-by-step approach that would work too:

如其他答案所示,使用正则表达式解决它是最简单的。这是一个循序渐进的方法,也可以工作:

$result = explode(",", $query);  // Split the string by commas
$lat = trim($result[0]);         // Clean whitespace
$lon = trim($result[1]);         // Clean whitespace

if ((is_numeric($lat)) and (is_numeric($lon))) echo "Valid coordinates!";

This solution will accept arbitrary data after a comma:

此解决方案将在逗号后接受任意数据:

 "-33.805789,151.002060,ABNSBOFVJDPENVÜE";

will pass as ok.

会过得好。

As Frank Farmer correctly notes, is_numeric will also recognize scientific notation.

正如Frank Farmer正确指出的那样,is_numeric也将识别科学记数法。

#3


0  

/^-*\d*\.\d+,[\b]*-*\d*\.\d+$/

#4


0  

The regex approach can't really validate that longitude and latitude are valid, but here's one that would be more precise than the others posted already:

正则表达式方法无法真正验证经度和纬度是否有效,但这里的方法比已经发布的其他方法更精确:

/^\s*-?\d{1,3}\.\d+,\s*\d{1,3}\.\d+\s*$/

This would reject some strings that others' solutions would allow, such as

这会拒绝其他人的解决方案允许的一些字符串,例如

-1-23-1-,210-
--123.1234,123.1234

But it would still allow invalid values like this:

但它仍然会允许这样的无效值:

361.1234, 123.1234

Your best bet -- if you need serious validation -- is to create a class to store and validate these coordinates.

您最好的选择 - 如果您需要认真验证 - 是创建一个类来存储和验证这些坐标。