RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Validate Latitude and Longitude With PHP

The following functions will validate latitude and longitude data. The functions support a number of geo-related articles on our website Tag: maps.

1
<?php 
2
/*
3
 Validate Latitude and Longitude
4
 http://www.beliefmedia.com/code/php-snippets/validate-latitude-longitude
5
*/
6
 
7
function beliefmedia_valid_lat($latitude) {
8
 return (preg_match("/^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}$/", $latitude)) ? true : false;
9
}
10
 
11
function beliefmedia_valid_lng($longitude) {
12
 return (preg_match("/^-?([1]?[1-7][1-9]|[1]?[1-8][0]|[1-9]?[0-9])\.{1}\d{0,6}$/", $longitude)) ? true : false;
13
}

Returns false if the value does not match.

The function validates again 6 decimal points (which you may obviously change). To force the six digits, and to further clean the input data, using $latitude = number_format($latitude, 6); (for both latitude and longitude) before validation may assist. In addition to truncating the decimals (or adding them), the function acts a little like the abs() function in that it'll remove invalid characters. Because the decimals are required, the function will also add them if omitted.

We've also used the following expressions... essentially achieving the same thing.

1
<?php 
2
/*
3
 Validate Latitude and Longitude
4
 http://www.beliefmedia.com/code/php-snippets/validate-latitude-longitude
5
*/
6
 
7
function beliefmedia_valid_lat($latitude) {
8
  return preg_match('/^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$/', $latitude);
9
}
10
 
11
function beliefmedia_valid_lng($longitude) {
12
  return preg_match('/^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,6})?))$/', $longitude);
13
}

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment