How to verfiy postcode using php? - php

I'm having postcode data in below format:
I need to map customer with their provided postcode.
EG. If customer enters postcode 36421 than it will assign to its
related representative that is "John".
If customer enter postcode 36222 than it will assign to related
representative that is "Sam".
If customer is from swiss country than it will assign to only one
representative as per data in image.
I'm confused how can I map customer.
I also tried with sub_str like,
$postCode = $_POST['postcode'];
$postCode3Digit = substr($postCode, 0, 3);
But it breaks.
Also tried with regex but at same moment i think than I have to write regex for every single postcode :(
I tried with switch case but it seems to be taking more time.
What will be the best way to achieve this ? Any help would be appreciated.

I would look it up by using regex, because ZIP-data often are a little too complex for substr() only. So what I would do is to loop trough the persons and to compare the zip of the person with the zip-input. Keep in mind that you need to replace the X (in your script) with dots. Dots are recognized as one single character in regex.
$search = '34567';
$reps = array('adam' => '12...',
'bdam' => '23...',
'cdam' => '345..',
'ddam' => '346..');
foreach($reps as $name => $zip){
if(preg_match('/^'.$zip.'$/', $search)){
echo $name; break;
}
}

Related

how to get the spesific word from different string in php

for getting spesific word that i will use it for search term.
if i have a random word like :
$str="297819 - 123C, WHITLEY ROAD";
that $str is got from adrress which is got from autocomplete jQuery, every address is defined into 2 part. first 297819 are "code", and 123C, WHITLEY ROAD is the "address".
i know hot to get the "code" with this following code :
$b = substr($str,0,strpos($str,"-")-1)
it will be return the "code" only.
guys can you help me how to get the address?
p.s every code and address will be disperated by -
Code and address separate by - so you can split it Using explode() as
$str="297819 - 123C, WHITLEY ROAD";
$data=explode("-",trim($str));// split string with -
echo $code=trim($data[0]);// get code
echo $address=trim($data[1]);// get address
And use trim() to remove unnecessary space

Build phone number regex filter

I need to make phone number filter using reg ex.
here is my code
<?php
$LIST =['1234123', '0121234123', '123-1234', '1234-123','0123-123-1234',
'123 4123', '012a1234123', '123x1234', '12341-23', '012-3123-1234'];
$regex = '/(\d{3}-\d{4}$)|(\d{4}-\d{3}$)|(\d{3,4}-\d{3}-\d{4}$)|(\d{3,4}-\d{4}-\d{3}$)|(^[0-9]{7,10}$)/';
foreach ($LIST as $key => $value)
{
echo $value.">>".(preg_match($regex, $value)).'<br/>';
}
?>
and here is the results
1234123>>1
0121234123>>1
123-1234>>1
1234-123>>1
0123-123-1234>>1
123 4123>>0
012a1234123>>0
123x1234>>0
12341-23>>0
012-3123-1234>>1
What makes me confused is the last one.
I want to make the last want will give 0 result while maintain the rest result.
Working with phone numbers is not that easy. The phone numbers may be different from country to country, from one operator to another, etc.
Instead of using an regex let me recommend you to use a library for phone numbers: libphonenumber-for-php.
The library can be used for parsing, formatting, storing and validating international phone numbers. This library is based on Google's libphonenumber.

preg_match php returning text between two search paramaters

if this is a duplicate I apologise but i couldnt find anything on google after hours of searching, im pretty new to string manipulation and dont really know the correct terminology to find the information i want.
Basically I am manipulating this string
Date Time Name IP UniqueID
$line = 02.12.2013 16:00:03: Connor Bergolio (75.13.15.229:5557) fcfd6ba862c7461a88e2b13babc691dd
So I am trying to retreive the name, However as they can choose whatever name they want, it could have 1 space or 10 spaces so explode is out of the question.
Now I was wondering if it is possible to run a pregmatch using 2 variables. that will return the information between
$pattern1 = '$time, $ip';
preg_match($pattern1, $line, $name);
Looking at that, its way off, but I'm pretty much at a loss
Im using `$IPpattern = '/([0-9-():.)]{19,23})/';
to get the IP maybe using that and a search for time together?
Thanks in advance`
The following pattern will work:
preg_match('/^(.{19}): (.+?) +\(([0-9:.]+)\) ([a-f0-9]+)$/', $line, $matches);
$date = $matches[1];
$name = $matches[2];
$ip = $matches[3];
$uniqueId = $matches[4];
Not knowing the vaild characters allowed for a username, or any of the rules governing the format of Date and Time fields, the following should work:
.*(?:\d\d:){3}\s*\K.+(?=\s?\()
EXPLAINED
.*(?:\d\d:){3}\s*\K - Match everything up to Time field then drop it with \K
.+(?=\s?\() - Match anything one or more times up to but not including the first bracket found
It's not efficient though :(
I'm using this regex to retrieve the name:
/[\d{2}.]{2}\d{4}\s(\d{2}:){3}\s(.+)(\s\(.+)/
Have a play, the second result from this is your name.

PHP check postal code in MySQL Database

I am trying to write a script to check someone's details they enter in a HTML form.
I have the code fine but i have realised if they type the postcode in a different format to whats in the database its going to error.
do i just take the POSTed postcode and change the format, if so whats the best way of making whats typed one format?
for example, in the database there is a row with the postal code SS9 5LY but if someone types it as SS95LY or ss9 5ly it wont match
First, take a look to ZIP (POSTAL) Code Validation Regex. Also, try to use a database records in your validation.
Have thousants of approachs to solve your problem.
You can use masked input, you can validate the input text before send to database using
RegularExpression. And many others.
You can use preg_replace() to remove any unwanted characters from the input, convert everything to upper case and finally add the space after the third character using substr():
$postcode = preg_replace("/[^a-zA-Z0-9]+/", "", $postcode);
$postcode = strtoupper($postcode);
$postcode = substr($postcode, 0, 3) . ' ' . substr($postcode, 3);

php extract UK postal code and validate it

I have some text blocks like
John+and+Co-Accountants-Hove-BN31GE-2959519
I need a function to extract the postcode "BN31GE". It may happen to not exist and have a text block without postcode so the function must also validate if the extracted text is valid postcode .
John+and+Co-Accountants-Hove-2959519
The UK Government Data Standard for postcodes is:
((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))
Edit: I had the above in some (personal) code with a reference to a now non-existence UK government web page. The appropriate British Standard is BS7666 and information on this is currently available here. That lists a slightly different regex.
Find below code to extract valid UK postal code. It return array if post code found otherwise empty.
<?php
$getPostcode="";
$str="John+and+Co-Accountants-Hove-BN31GE-2959519";
$getArray = explode("-",$str);
if(is_array($getArray) && count($getArray)>0) {
foreach($getArray as $key=>$val) {
if(preg_match("/^(([A-PR-UW-Z]{1}[A-IK-Y]?)([0-9]?[A-HJKS-UW]?[ABEHMNPRVWXY]?|[0-9]?[0-9]?))\s?([0-9]{1}[ABD-HJLNP-UW-Z]{2})$/i",strtoupper($val),$postcode)) {
$getPostcode = $postcode[0];
}
}
}
print"<pre>";
print_r($getPostcode);
?>
Use a regex: preg_grep function,
I don't know the format of english postcodes but you could go with something like:
(-[a-zA-Z0-9]+-)+
This matches
"-Accountants-"
"-BN31GE-"
You can then proceed at taking always the second value or you can enhance you regex to match exactly english postcodes, something like maybe
([A-Z0-9]{6})

Categories