-5

I need to validate a complete address using php and regex. This is what i have created so far.

<?php

$input = '15 Gordon St, 3121 Cremorne, Australia';
if (!preg_match('/^[^@]{1,63}@[^@]{1,255}$/', $input)) {
    echo 'Not matched';
} else {
    echo 'Matched';
}

sample Address.

15 Gordon St, 3121 Cremorne, Australia

This is returning false.

#, Street name, Zip Code, City, Country is the address format.

dev1234
  • 5,376
  • 15
  • 56
  • 115
  • 9
    That RegEx is for email addresses... makes me wonder whether you just copy-pasted it, or you actually know RegEx. – Siguza Jul 28 '15 at 06:18
  • 2
    if this is user-supplied free text, you may find this task almost impossible. if you have the time, an interesting read is "falsehoods programmers believe about addresses" https://www.mjt.me.uk/posts/falsehoods-programmers-believe-about-addresses/ – frymaster Jul 28 '15 at 06:24

4 Answers4

4

Your regex looks very much like an email-address validation one, which strongly suggests you don't know regex at all.
I'm not even gonna argue that this is a "gimme teh codez" question anymore, but honestly, you should learn regex if you intend to use it.

\d+ = one or more digits
[a-zA-Z ]+ = one or more of: upper case letter, lower case letter, space

Therefore:

preg_match('/^\\d+ [a-zA-Z ]+, \\d+ [a-zA-Z ]+, [a-zA-Z ]+$/', $input)

Which could also be shortened to:

preg_match('/^(?:\\d+ [a-zA-Z ]+, ){2}[a-zA-Z ]+$/', $input)
Siguza
  • 21,155
  • 6
  • 52
  • 89
1

I'm not a php specialist but does this help?: PHP Validation: 1st Line of address and postcode

else you can try:

Here's a free and sort of "outside the box" way to do it. Not 100% perfect, but it should reject blatantly non-existent addresses.

Submit the entire address to Google's geocoding web service (https://developers.google.com/maps/documentation/geocoding/intro). This service attempts to return the exact coordinates of the location you feed it, i.e. latitude and longitude.

In my experience if the address is invalid you will get a result of 602 from the service. There's definitely a possibility of false positives or false negatives, but used in conjunction with other consistency checks it could be useful.

(Yahoo's geocoding web service, on the other hand, will return the coordinates of the center of the town if the town exists but the rest of the address is bogus. Potentially useful as long as you pay close attention to the "precision" field in the result).

Community
  • 1
  • 1
1

Given your examples: preg_match('/^[0-9]+ [^,]+, [0-9]+ [^,]+, [a-z]+$/', $input)

Given your format: preg_match('/^[0-9]+, [^,]+, [0-9]+, [^,]+, [a-z]+$/', $input) (there are two commas in your format description, which aren't in your examples)

[^,] = everything, but ,

CarHa
  • 1,148
  • 11
  • 31
0

Here is the solution I came up with: This function assumes that the street address is being validated individually; separately from the city, state and zip, but including a full address will not cause validation to fail. I just want to make sure the street address begins with a numerical and includes a street name. I also want to make sure that if the street name is a number (eg. 35th St.) that validation doesn't fail.

// Checks for a street number and street name.
// Allows street names to begin w/ numbers.
// eg. '123 35th St.' will pass
// eg. 'West 35th St.' will not pass
function validate_street_address($string) {
$check_pattern = '/\d+ [0-9a-zA-Z ]+/';
$has_error = !preg_match($check_pattern, $string);
// Returns boolean:
// 0 = False/ No error
// 1 = True/ Has error
return $has_error;
}
AnarchyOutlaw
  • 163
  • 2
  • 6