Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phone number format received from twilio

Tags:

php

mysql

twilio

I am storing sms received from twilio in a database so I can use them later. When I did this in the sandbox it worked. However when I upgraded to a regular phone number the number received is the same as was sent to, but +1 (or for xxxxxxxxxx where the x's are the original number, it looks more like 1xxxxxxxxxx+)

I therefore changed the mysql_query to the following: but it is still not working. What can be done to recognize that this is the original phone number?

<?php
    $starttime = time(); 
    $number = $_POST['number'];

    $number1 = "1" . $number;
    $number2 = $number . "1";
    $number3 = "+1" . $number;
    $number4 = $number . "+1";
    $number5 = "+" . $number . "1";
    $number6 = "1" . $number . "+";
    $number7 = $number."1+";

    $received = mysql_query("SELECT * FROM sms_received 
                WHERE (responder='$number' OR responder='$number1' 
                     OR responder='$number2' OR responder='$number3' 
                     OR responder='$number4' OR responder='$number5'
                     OR responder='$number6' OR responder='$number6')
                AND (body='y' OR body='yes' OR body='Y' OR body='Yes' OR 'yea' OR 'Yea')
                AND timestamp BETWEEN ".date('Y-m-d H:i:s', strtotime($starttime))." AND NOW()"); 
?>

But still, nothing is being received. Any ideas how else I can check whether an sms has been received from the user? I can see in the database that it's there... but the mysql isn't finding it. It worked before, when the number sent was identical to the number received from, but with the added +1 it screws it up. (the code before just had WHERE responder = '$number' and it worked, but the additional variables didn't help it). Does this code have too many OR's? Is that even a problem?

UPDATE:

Thanks, here is the function I'm using to strip the number down to xxxxxxxxxx format, before saving it to the database:

function checkPhone($responder){
    $items = Array('/\ /', '/\+/', '/\-/', '/\./', '/\,/', '/\(/', '/\)/', '/[a-zA-Z]/');
    $clean = preg_replace($items, '', $responder);

    if (substr($clean, 0, 1) == "1") {
        return substr($clean, 1, 10); 
    }
    else {
        return substr($clean, 0, 10);   
    }
}

$number = checkPhone($responder);
like image 651
Lucy Weatherford Avatar asked Jan 09 '12 19:01

Lucy Weatherford


People also ask

How do you tell if a number is from Twilio?

Text +1 (855) 747-7626* with a phone number to check if it is from Twilio. That's +1 (855) 747-ROBO. If you're getting spam calls or texts from a Twilio number, this text hotline will help you report it.

What is the correct way to format a phone number?

To format phone numbers in the US, Canada, and other NANP (North American Numbering Plan) countries, enclose the area code in parentheses followed by a nonbreaking space, and then hyphenate the three-digit exchange code with the four-digit number.

When using +44 do I drop the 0?

Calling these numbers from outside the UK Depending on the service provider within the country of origin, you may be able to call an 08 or 03 number from outside the UK. To do so, you will need to drop the 0 from the start of the number and instead dial +44 followed by the rest of the number.

What's in a number Twilio?

A Twilio number can include different communication capabilities such as voice, SMS, MMS, and fax. You can programmatically manage these numbers and build complex IVR apps and chatbots using Twilio's REST APIs.


1 Answers

Twilio returns numbers in a format called E.164, which is an internationally recognized standard for phone number formatting.

In general, it's best practice to standardize the number to E164 BEFORE you store it in the database. That way you don't have to worry about storing different data with two different copies of the same number - eg 925-555-1234 and (925) 5551234.

Google has a libphonenumber library that will convert numbers for you. It works with Javascript, C++, Java, and Python.

If you are using PHP, and only using US/Canadian numbers, you can write a function to normalize phone numbers, that does something like the following:

- Strip out all non number characters from the phone number 
  (parentheses, dashes, spaces) - you can use a function like preg_replace
- if the phone number begins with a +1, do nothing
- if the phone number begins with a 1, add a +
- else, add a +1 to the beginning of the number.
- finally, store it in the database.

I hope that helps - please let me know if you have more questions.

Kevin

like image 55
Kevin Burke Avatar answered Sep 19 '22 15:09

Kevin Burke