Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中验证纬度和经度时出错_Php_Validation - Fatal编程技术网

在php中验证纬度和经度时出错

在php中验证纬度和经度时出错,php,validation,Php,Validation,我正在尝试使用函数验证纬度和经度 这是我正在使用的函数: // Function to validate latitude function isValidLatitude($latitude){ if (preg_match("/^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}$/", $latitude)) { return true; } else { return false; } } 我这样

我正在尝试使用函数验证纬度和经度

这是我正在使用的函数:

// Function to validate latitude
function isValidLatitude($latitude){
    if (preg_match("/^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}$/", $latitude)) {
            return true;
    } else {
            return false;
    }
}
我这样称呼它:

// Check for the "latitude":    
if (!empty( $_POST['geolat'])) {    
    if (isValidLatitude($_POST['geolat'])) {
        $_SESSION['geolat'] = $_POST['geolat']; 
    } else {
        $error_alert[] = "Find your location correctly";
    }       
} else {
    $error_alert[] = "You didn't find your locaiton on google map";
}   
但这对我不起作用。它总是会:

$error_alert[] = "Find your location correctly";
我的
$\u POST
数组如下所示:

Array
(
    [rest_location] => Colombo
    [geolat] => 6.929677319820927
    [geolng] => 79.86519121166998
    [submitted] => TRUE
)
1
谁能告诉我这有什么问题吗

多谢各位

你的帖子价值-

[geolat] => 6.929677319820927
与您使用的正则表达式不匹配。(允许小数点后有1到6位)

因此,您可以将正则表达式更改为-

^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,20}$
                                 ^^ Changed from 6 to 20

将您的
geolat
值更改为小数点后的六位四舍五入-

$_POST['geolat']) = floor($_POST['geolat']) * 1000000)/1000000

将您的函数更改为如下所示:

function isValidLatitude($latitude){
    if (preg_match("/^-?([1-8]?[1-9]|[1-9]0)\.{1}\d+$/", $latitude)) {
            return true;
    } else {
            return false;
    }
}

也就是说,
{1,6}
表示只允许1到6个数字
+
表示允许1个或任意数量的数字

您可以发布进入
isValidLatitude
的内容吗?在这种情况下-
$\u POST['geolat']
我需要使用上述函数验证此
[geolat]
。@user3733831您能简单地解释一下纬度格式是什么吗?您是如何构建regexp@user4035的,当用户选择一个位置时,这个纬度和经度会动态生成。所以我需要这些数据存储在Mysql中。在运行INSERT之前,我需要验证这些数据。这就是我使用上述函数的原因。Kamehameha,如果我使用第二种方法,应该
geolat
更改为“6.929677”?@user3733831是的。它将被修改。这是我如何使用它的“$geolat=floor($u POST['geolat'])*1000000)/1000000;如果(isValidLatitude($geolat)){$_SESSION['geolat']=$_POST['geolat'];}`但是
纬度
像这样存储在SESSION中-
[geolat]=>6.928484466599483
。它没有被修改。@user3733831应该可以工作。但它违背了验证的目的。您正在将原始文件粘贴到会话中。它需要是
$\u SESSION['geolat']=$geolat
function isValidLatitude($latitude){
    if (preg_match("/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?);[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/", $latitude)) {
            return true;
    } else {
            return false;
    }
}