snippetphpMinor
Address filter with PHP using regex
Viewed 0 times
addresswithphpfilterusingregex
Problem
I developed a PHP function to get a not formatted address and split it in a street name and number.
Following are some patterns of received addresses
I'm using regex to identify the pattern and then splitting it. Here is the function (the code is commented for better understanding):
```
NULL,
'number'=>NULL,
'complement'=>NULL);
//firstly, erase spaces of the strings
$addressWithoutSpace = str_replace(' ', '', $address);
//discover the pattern using regex
if(preg_match('/^([0-9.-])+(.)*$/',$addressWithoutSpace) === 1) {
//here, the numbers comes first and then the information about the street
$info1 = preg_split('/[[:alpha:]]/', $addressWithoutSpace);
$info2 = preg_split('/[0-9.-]/', $address);
$return['number'] = $info1[0];
$return['street'] = end($info2);
}
elseif(preg_match('/^([[:alpha:]]|[[:punct:]])+(.)*$/',$addressWithoutSpace) === 1) {
//here, I have a alpha-numeric word in the first part of the address
if(preg_match('/^(.)+([[:punct:]])+(.)([0-9.-])$/',$addressWithoutSpace) === 1) {
if(preg_match('/,/',$addressWithoutSpace) === 1) {
//have one or more comma and ending with the number
$info1 = explode(",", $address);
$return['number'] = trim(preg_replace('/([^0-9-.])/', ' ', end($info1)));//the last element of the array is the number
array_pop($info1);//pop the number from array
$return['street'] = str_replace(",", "",implode(" ",$info1));//the rest of the string is the street name
}
else {
Following are some patterns of received addresses
- StreetName Number
- SrtreetName, Number
- Number StreetName
- Number-Number StreetName
- StreetName, Number, Complement
- StreetName Number/Number
- StreetName Number - ZipCode (ZipCode could be ignored)
- StreetName (without number)
I'm using regex to identify the pattern and then splitting it. Here is the function (the code is commented for better understanding):
```
NULL,
'number'=>NULL,
'complement'=>NULL);
//firstly, erase spaces of the strings
$addressWithoutSpace = str_replace(' ', '', $address);
//discover the pattern using regex
if(preg_match('/^([0-9.-])+(.)*$/',$addressWithoutSpace) === 1) {
//here, the numbers comes first and then the information about the street
$info1 = preg_split('/[[:alpha:]]/', $addressWithoutSpace);
$info2 = preg_split('/[0-9.-]/', $address);
$return['number'] = $info1[0];
$return['street'] = end($info2);
}
elseif(preg_match('/^([[:alpha:]]|[[:punct:]])+(.)*$/',$addressWithoutSpace) === 1) {
//here, I have a alpha-numeric word in the first part of the address
if(preg_match('/^(.)+([[:punct:]])+(.)([0-9.-])$/',$addressWithoutSpace) === 1) {
if(preg_match('/,/',$addressWithoutSpace) === 1) {
//have one or more comma and ending with the number
$info1 = explode(",", $address);
$return['number'] = trim(preg_replace('/([^0-9-.])/', ' ', end($info1)));//the last element of the array is the number
array_pop($info1);//pop the number from array
$return['street'] = str_replace(",", "",implode(" ",$info1));//the rest of the string is the street name
}
else {
Solution
Looking very quickly, it seems like a frail code.
Still, it works as expected.
But I saw a few things to improve:
It sounds like it wil fetch the address somewhere, but that isn't the case...
The address is being parsed. A name like
PHP isn't case-sensitive regarding function names.
If you write
My recommendation goes on
Avoid this:
Be explicit. I have no idea what
-
You over-use
You have this line:
You should use
This will improve the performance by quite a bit.
-
Please, don't mix Portuguese with English.
Your
Please, only and only give English names to your variables.
Everybody will thank you.
-
Right on top, you "normalize" your input:
But you use that
-
Avoid closing the PHP tag on a file that only has PHP code
This will avoid frustrations due to a forgotten whitespace after the closing tag.
Many services, like Github, add a newline to the end.
PHP automatically ignores 1 and only 1 whitespace after
If you leave 1 more newline by mistake, you can seriously break stuff everywhere.
Just remove the
Still, it works as expected.
But I saw a few things to improve:
- You called your function
getInfoAddress().
It sounds like it wil fetch the address somewhere, but that isn't the case...
The address is being parsed. A name like
parseAddress() seems better.- But, your function casing is wrong, in my opinion.
PHP isn't case-sensitive regarding function names.
If you write
parseaddress(), you may have problems in the future, if you need to change something.My recommendation goes on
parse_address()- Be explicit regarding your regular expressions.
Avoid this:
/^([[:alpha:]]|[[:punct:]])+(.)*$/Be explicit. I have no idea what
punct means. It is ponctuation?-
You over-use
preg_match.You have this line:
if (preg_match('/,/',$addressWithoutSpace) === 1){You should use
strpos() for this:if (strpos($addressWithoutSpace, ',') !== false){This will improve the performance by quite a bit.
-
Please, don't mix Portuguese with English.
Your
$endereco variable should have other name.Please, only and only give English names to your variables.
Everybody will thank you.
-
Right on top, you "normalize" your input:
$addressWithoutSpace = str_replace(' ', '', $endereco);But you use that
$endereco variable everywhere. Maybe it was by mistake?-
Avoid closing the PHP tag on a file that only has PHP code
This will avoid frustrations due to a forgotten whitespace after the closing tag.
Many services, like Github, add a newline to the end.
PHP automatically ignores 1 and only 1 whitespace after
?>, but not more.If you leave 1 more newline by mistake, you can seriously break stuff everywhere.
Just remove the
?> at the end.Code Snippets
if (preg_match('/,/',$addressWithoutSpace) === 1){if (strpos($addressWithoutSpace, ',') !== false){$addressWithoutSpace = str_replace(' ', '', $endereco);Context
StackExchange Code Review Q#114397, answer score: 5
Revisions (0)
No revisions yet.