如何确定一个PHP字符串是否只包含经度和纬度 [英] How to determine if a PHP string ONLY contains latitude and longitude

查看:112
本文介绍了如何确定一个PHP字符串是否只包含经度和纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ query =-33.805789 

我必须使用可能包含Lat / Long数据的字符串。 ,151.002060\" ;
$ query =-33.805789,151.002060;
$ query =OVER HERE:-33.805789,151.002060;

就我的目的而言,前2个字符串是正确的,但最后一个不是。我试图找出一个匹配模式,匹配经纬度,长逗号,逗号和空格。但是如果字符串中包含数字,空格,点号,减号和逗号之外的任何内容,那么它应该会失败。



希望这是有道理的,而TIA !

解决方案

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

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

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!

解决方案

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

这篇关于如何确定一个PHP字符串是否只包含经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆