I have a field in which a user can search a MySQL database for email, phone, and username. All numbers in the database are 10 digit (1231231234) format.
IF (big if there) the user enters a phone number in the following format(s) I want it to be sanitized into just a 10 digit string as it correlates in the database:
(123)123-1234
123-123-1234
123.123.1234
+1(123)123-1234
11231231234
Usernames and emails are allowed to have . and - characters. Hence I don't know how to use PHP to determine if it is in one of these formats and then sanitize it accordingly. Ideas?
This will remove anything you need to remove from phone numbers or any other value. Simply update the array if you need to remove other things.
$string = str_replace(array('-',' ','.','(',')',',','"','+'),'',$string);
libphonenumber can properly reformat phone numbers to a common format, adding international codes where appropriate, and then it can be stored in the database as a simple, searchable string.
Related
I am currently working with an Oracle database trying to validate phone numbers within my PHP code. I have one column "PHONE 1" that contains a string phone number. This phone number may contain a leading country code "1" or a trailing phone extension (usually 4 digits). I need PHONE 1 to only contain the 10 digit phone number and then if it has country code or extension, I need to remove them and store them in separate columns which are currently empty within my Oracle database ("PHONE 1 COUNTRY CODE" and "PHONE 1 EXTENSION"). I have found a way to remove the leading country code, but I am not sure how to remove the trailing extension. I looked into possibly using the explode() function but cannot figure it out. Here is my code that I am using to remove the leading 1:
while($row=oci_fetch_array($array, OCI_ASSOC+OCI_RETURN_NULLS)){
//VALIDATE PHONE NUMBERS AND COUNTRY CODES
if(isset($row["PHONE 1"])){
if (strlen($row["PHONE 1"])>10){
$row["PHONE 1"] = preg_replace("/^1/", '',$row["PHONE 1"]);
$row["PHONE 1 COUNTRY CODE"]="1";
if(strlen($row["PHONE 1"])>10){
//insert code here that will remove the extension and add it to the column $row["PHONE 1 COUNTRY CODE"]
}
}
}
I think that using adding the second if statement within the other will be the easiest way to remove the extension. Essentially, this should say if there are more than 10 digits, remove the leading 1, and then if there are still more than 10 digits, remove the trailing extension. I just need to figure out how to code the latter. Any input on how to improve my current code or add the new one will be appreciated.
How you handle the phone numbers varies, depending on what you're starting with and what you want to end up with.
A good strategy for this type of problem is to "divide & conquer".
Divide your data into two groups - for example, those with extensions & those without.
Take the ones with extensions and divide them further, say into groups like "8005551212x100" (ie. with an 'x' denoting the extension) and others. Now you know how to handle the first group - split the string on the 'x', put the phone number into one column in your database and the extension into another. Now you have a few more phone numbers (with a variety formats, probably), but the extensions have been taken care of.
Methods for handling the phone numbers include:
explode - good for separating strings with clearly defined delimiters. Eg. explode("x", "8005551212x100") == array("8005551212", "100")
substr - good for strings where specific information appears at specific locations in the string. Eg. substr("8005551212",3,3) == "555"
regular expressions - can be good for variable data. Eg. for phone numbers where the extension is delimited by "x" or "ext" or "extension": preg_match("/^[0-9]+(x|ext|extension)[0-9]+$/", "8005551212ext100"). Be careful, though, to resist the temptation to write one regular expression that covers ALL types of numbers in your database - it's probably possible, but you're drive yourself crazy trying to write & debug an insanely long regular expression. That's why I suggest dividing your data up into groups of similar formats.
I need to search in a database for a phonenumber. However, I don't know how the phone number is stored in the database, this can be in different ways, like:
0123456789
012 3456789
012 34 56 78 9
012-3456789
The string that I have to look for is always formatted like
0123456789
My query now looks like:
SELECT * FROM account WHERE phonenumber = '0123456789'
But this ofcourse only works when the phonenumber is formatted like the search string. How do I use a regex of other function to search for all kind of formatted phonenumbers?
Use Mysql REGEXP. This is a basic example of how You can achieve that. Works with every number format in Your example.
Think of better regexp to be more precise.
SELECT * FROM account WHERE phonenumber REGEXP '012( |-|)34( |)56( |)78( |)9';
While it is possible to perform REGEXP's in MySql, I don't think its a very solid solution and the expression will be hard to tune.
SELECT * FROM account WHERE phonenumber REGEXP '^([0-9]{1}( |-|)){9}[0-9]{1}$'
A good tool to test expressions is this site: http://www.spaweditor.com/scripts/regex/index.php
The trick is to normalize your data before you enter in in your database which means strip the telephone number of all non numeric characters.
You'll need to fix the numbers already in the table too.
Best way is to store the data normalized, i.e.: Country + area + number separately .
I have within a form a textbox named PO_Number. The form submit by post to another page the textbox value.
In the second page I get $_POST['PO_Number'] and enter in MySQL.
MySQL field is varchar(15). As soon as the string of PO_Number starts with a letter or a number everything is OK.
The problem: sometimes the PO (Purchase Order) number start with 00 or 000 and it is stored with a comma before the 00
For example:
GH93737 - works
9087893 - works
0011132 - entered in database as ,0011132 (see the comma?)
The insert looks normal:
mysql_query("INSERT INTO table_name (PO_Number, ....) VALUES ('".$_POST['PO_Number']."',......)");
Many thanks for your suggestions and your help.
I'm wondering if this has something to do with your browser/server character encoding and how it's interpreting those specific numbers because all of those leading zeros and ones might be getting interpreted as a binary number?
Here's some brief info on that point:
A character encoding tells the computer how to interpret raw zeroes and ones into real characters. It usually does this by pairing numbers with characters.
http://htmlpurifier.org/docs/enduser-utf8.html
I have a form in which users can enter prices for items. Ideally I want the user to be able to add prices in whatever method feels best to them and also for readability. I then need to convert this to a standard float so that my web service can calculate costs etc.
The part I'm struggling with is how to take the initial sting/float/int of currency and convert it into a float.
For example:
UK: 1,234.00
FRA: 1 234,00
RANDOM: 1234
RANDOM2: 1234.00
All of those have slightly different formats.
Which I would want to store as:
1234.00
I will then store the result in MySQL database as a DECIMAL.
Any help would be great.
Assuming you're using MySQL, use the DECIMAL or NUMERIC type are the correct types used for storing currency.
Float's are susceptible to rounding errors and have a limited precision.
The formatting for display should be handled by PHP.
If storing in DB, you should of course store a currency code - which can be used when retrieving to tell PHP how to display it
Couldn't you use:
floatval($AnyVar)
In a case where you'd like to accept so many different formats it's a bit tricky to get it right.
Now we can just use a simple regex to get the decimal and full parts of the value:
/^([0-9,. ]+?)(?:[.,](\d{1,2})$|$)/
The regex will capture the full part of the number + a decimal part, separated with a , or a . and which has one or two numbers.
The capture group 1 will contain the full part, and group 2 the decimal part (if any).
To get your number, you just need to filter out all non-numeric characters from the full part, and join the filtered full and decimal parts together.
If you want to make it more foolproof, you probably should implement something on the client-side to guide the user to input the value in the correct format.
How do i accept values from user that contain multiple points like 1.2.1...if i use float--1.2.1 gets converted to 1.2.
Thanks.
the simple answer: if u want multiple points DONT USE FLOAT :-)
use something like varchar or text instead
Treat it as text.
A float is used to represent Real numbers, and "1.2.1" is not a Real number.
Or, if "1.2.1" is simply a grouping of numbers, you could split the input of "1.2.1" into three separate numbers using the period as a delimiter, and store them as distinct numbers.