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

发布于 2024-08-23 08:31:31 字数 314 浏览 3 评论 0原文

我必须使用可能包含纬度/经度数据的字符串,如下所示:

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

就我的目的而言,前两个字符串是正确的,但最后一个字符串不正确。我正在尝试找出一种匹配模式,该模式将匹配以逗号分隔的纬度和经度,或者逗号和空格。但是,如果字符串中包含除数字、空格、点、减号和逗号之外的任何内容,则匹配失败。

希望这是有道理的,TIA!

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

$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.

Hope this makes sense, and TIA!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌生 2024-08-30 08:31:31
^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$

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

^[+-]?\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.

紫瑟鸿黎 2024-08-30 08:31:31

按照其他答案中的建议,使用正则表达式解决是最简单的。这是一个也可行的分步方法:

$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!";

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

 "-33.805789,151.002060,ABNSBOFVJDPENVÜE";

将正常通过。

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

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.

零度℉ 2024-08-30 08:31:31
/^-*\d*\.\d+,[\b]*-*\d*\.\d+$/
/^-*\d*\.\d+,[\b]*-*\d*\.\d+$/
烟花肆意 2024-08-30 08:31:31

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

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

这将拒绝其他解决方案允许的某些字符串,例如

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

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

361.1234, 123.1234

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文