I have seen a few question on SO similar to what I require but nothing seems to fit the bill.
I am in the position where I need to deal with a call record and determine the country using the phone number. The number dialed can be any country for example:
44 7899455120 - UK
34 965791845 - Spain
355 788415235 - Albania
Obviously the world would be great if all calling codes were two digits but this is not the case. Currently I have a database full of countries and their relevant codes and in order to match I need to effectively take the first digit of the number ie 4 for the UK example and do a query of the database eg:
SELECT * from countries WHERE code LIKE '4%'
This may give me for example 20 results. So I loop again and do say
SELECT * from countries WHERE code LIKE '44%'
This may give me say one result, now I can determine it is UK. Some codes however like Albania are three digits and require more loops and database calls. This seems quite rudimentary and inefficient but as is I cannot think of another way to achieve this. I realise three calls to a database may not seem like much but if you have 1000 calls to deal with they soon add up.
Looking at the following question:
What regular expression will match valid international phone numbers?
There seems to be some great information on validating a number against country codes, but not so much on determining the country code from a number. Any advice or suggestions on a cleaner method would be much appreciated.
Spaces in the phone are shown for clarity
A library exists that will parse a string of digits and reformat it to international standards (a number like 4402081231234 to '+44 20 8123 1234'). It will also return the Phone Number region, 'GB' or 'US' from a number, if there is the country code embedded in the number.
https://github.com/googlei18n/libphonenumber The original library is in Java, but there are also versions in Javascript, Python, Ruby and PHP, among others.
There is no overlap ambiguity in the country codes. Meaning: the country code 11 is illegal because 1 is assigned to North America. Similarly, 20 is Egypt and there are no other country codes that start with 20. And the country codes that start with 21 are all 3 digits.
Since the is no overlap ambiguity, you can directly search for the country code in one query for the phone number 12125551212 like this:
select country
, code
from countrycodes
where code in ('121', '12', '1')
Again, there are no country codes 121 or 12, so the only criteria that will match is the 1.
Assuming the phone will always look like that:
$phone = "355 788415235"; // Albania
$parts = explode(" ", $phone);
$code = $parts[0]; // First part separated by space, 355.
Then query by that directly. No regular expression needed.
If that's not the case, consider separating the country code from the number on the input level.
On your system, every phone number has white space after country code so you can use it to determine country.
Create a table which has all country codes. Lıke
id | country | code
1 | Turkey | 90
2 | Spain | 34
(There is a table for you: http://erikastokes.com/mysql-help/country.sql.txt )
Than explode your phone number. Delimeter is white space " ".
$phoneNumber = "355 788415235";
$countryCode = explode(" ",$phoneNumber); // it divides phone number to two parts.
$countryCode = $countryCode[0]; // it returns 355. We write index 0 because country code is first part.
//Now you can call your country by country code.
$sqlQuery ="SELECT country FROM yourTableName WHERE code = '$countryCode' ";
...
//it will works like a charm. Because i currently using this.
Related
I am supposed to find out what country the call is originated from by reading the country code out of the number.
Sample Calling Numbers:
+18098998343 [Dominican Republic] Code: +1809
+18006005001 [US] Code: +1
The Challenge is that the country code numbers are different from one to four digits. I have a list of all countries with their calling code in my mysql database. What could be the best possible way to detect the country code?
You could iterate over all known countrycodes and check, whether your phone number starts with. Once there are more than one hit, choose the longest hit.
I want to find country code from the phone number because phone number is without country code(which is my input) for instance my phone no is 9xxxxxxxxx and I get result that number is from India and it will display then +919xxxxxxxxx in php.Is there any way to find country code from phone number?
First you'd need a source for the data listing every country, its code and the number of digits and pattern of the number. Then you'd need to put that into an associative array, matching up each country with it's country code. Then you'd need a bunch of preg_matches using regular expressions to determine if the first digits plus the regular expression matched the country in the array. You've talking a huge project to program this from scratch.
You can build your own PHP function for a lookup like this, a list of all the Phone Netnumbers can be found on Wikipedia. If there is no net number it's not possible to track the country since countries can use the same phone numbers.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to validate phone number using PHP?
can anyone please help me know how to validate if the field value entered is a phone number using php...
I have used a variable $phone , datatype =varchar 10 in sql db
Now i want to validate that users enter only numbers in that field..
use preg_match
if(preg_match('/^(NA|[0-9+-]+)$/',$str)) {
*code here*
} else {
"code here"
}
One way to do this is with regular expressions. When validating phone numbers, it's easier on users if you accept accompanying characters, and filter them out yourself (-+()).
http://www.php.net/manual/en/function.preg-replace.php
$phone = preg_replace ( '/[+\\.\\(\\) ]/' , '' , $phone);
Once you've done that, checking for a match of 10 digits (assuming U.S. numbers with area code) can be done like so:
if(preg_match ( '/^\\d{10}$/', $phone) ) {
// Good match
}
http://www.php.net/manual/en/function.preg-match.php
Does is_numeric solve your problem?
Edit:
I wasn't aiming to solve OPs problem, merely hoping to give him/her pointers. However, reading the question closer makes me think that OP isn't being conscious of internationalisation issues. Her field is 10 characters long, so a number like +447970122467 (a valid British mobile number) would cause a failure. I'm going to assume they are in North America, and as such can assume that all numbers are in accordance with the North American Numbering Plan. The description of this, in words, is taken from that page:
Component Name Number ranges Notes
+1 ITU country calling code "1" is also the usual trunk code for accessing long-distance service between NANP numbers. In an intra-NANP context, numbers are usually written without the leading "+"
NPA Numbering Plan Area Code Allowed ranges: [2–9] for the first digit, and [0–9] for both the second and third digits. Covers Canada, the United States, parts of the Caribbean Sea, and some Atlantic and Pacific islands. The area code is often enclosed in parentheses.
NXX Central Office (exchange) code Allowed ranges: [2–9] for the first digit, and [0–9] for both the second and third digits. Often considered part of a subscriber number. The three-digit Central Office codes are assigned to a specific CO serving its customers, but may be physically dispersed by redirection, or forwarding to mobile operators and other services.
xxxx Subscriber Number [0–9] for each of the four digits. This unique four-digit number is the subscriber number or station code.`
That ought to be enough to get OP started on solving their problem. Sorry for being curt in my initial response.
Greetings,
I am planning a PHP and MySQL based app that will help in locating the correct page for a particular address in a map book. The map book uses a high and low address range to place sections of a street or highway in the book, each section with its own page (or sub-section of a page).
The user will enter the street address, house number separate from street name, and the desired result is to print details including the map page. What would be the best way to determine the corresponding high and low range based on the house number given in MySQL?
The table will be similar to this:
id, street_name, low_address, high_address, map_page
An example entry would be:
1, Elm Street, 1, 100, 30
Thanks!
Hate it when I end up posting my own solution, but MySQL's BETWEEN function fit the bill for this. Here is what i ended up with:
MySQL Prepared Statement
SELECT * FROM dispatch WHERE streetName = ? AND ? BETWEEN lowAddress AND highAddress LIMIT 1
PHP
$getInfo->execute(array($streetName, $houseNum));
I need to extract an address from a string
$string ="some text 9 th pizza tower 78 main Chennai 600001. and other information may be phone number etc";
From $string I want to extract only "9 th pizza tower 78 main Chennai 600001"
This Address format is not constant it may be in two different way
one is string variable another one is like this
$string1= "some text 9 th pizza tower main Chennai 600001. and other information may be phone number etc";
From here I need to extract "9 th pizza tower main Chennai 600001"
I don't think this is possible...extracting text from a plain text file is like asking for a tree if you're in the woods, "Which one?".
If the file is always in the same format, like:
Company Name 73
1st Cross Street, Hotel Chennai
-600000
someadditionalstuff
Then you've got a change, or if it is always separated with a special character (, . ; etc.). If it is always the same format (the one you showed above), then something like this might work:
([a-zA-Z0-9 ]*),([a-zA-Z0-9 ]*) XXX ([a-zA-Z0-9 ]*) (-[0-9]{6})
Group 1: Company Name
Group 2: Address
Group 3: City
Group 4: Zip-Code
Bobby
Sorry this is not possible. It may work for one website but not for others as there is no standard format in displaying a company address(or any address) on a web page.
Not an easy question and there isn't a magic AI code that can figure it out.
You must make some assumption , and look at a lot of data to find out if it's good ones.
for start - if you assume, every address ends with ZIP code, and you can search the string for 5 (or 6) digits and cut it after that.
TO find the start of the address is beyond my skills. maybe looking for the first number.
you need to check a lot of examples to figure out what would be the best patten that match most of them.
Yes its possible by using Google natural Language processing which is paid or you can open natural language processing which is open. But for open NLP there is no better documentation available .
Better refer from this URL :
https://opennlp.apache.org/