Phone number format received from twilio

I store sms received from twilio in the database, so I can use them later. When I did this in the sandbox, it worked. However, when I updated to my regular phone number, the received number matches the one that was sent, but +1 (or for xxxxxxxxxx, where x is the original number, it looks more like 1xxxxxxxxxx +)

So I changed mysql_query to the following: but it still doesn't work. 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('Ymd H:i:s', strtotime($starttime))." AND NOW()"); ?> 

But still nothing was received. Any ideas, how else can I check if sms is received from the user? I see in the database that it is there ... but mysql does not find it. It worked earlier when the number sent was identical to the quantity received from it, but with the added +1 it twists it. (the code before it was only WHERE responder = '$number' , and it worked, but the additional variables did not help him). Does this code have too much OR? Is that even a problem?

UPDATE:

Thank you, here is the function I use to split the number to xxxxxxxxxx format before storing it in 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); 
+6
source share
3 answers

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

In general, it is best to standardize the number to E164 before you store it in the database. Thus, you do not have to worry about saving different data with two different copies of the same number - for example, 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 use PHP and use only US / Canada numbers, you can write a function to normalize phone numbers, which does something like this:

 - 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 this helps - let me know if you have further questions.

Kevin

+6
source

Your last or excess $number6 , it should be $number7 .

In addition, you can do several different things, for example in :

 responder in ('$number', '$number1', '$number2', '$number3', '$number4', '$number5', '$number6', '$number7') 

Or something like this:

 responder like '%$number%' 
0
source

Use regex.

 preg_match_all("{[0-9]+}",$number,$m); $norm_num="+".implode($m[0]); if(strlen($norm_num)<6) exit('Too short!'); mysql_query("SELECT * FROM sms_received WHERE responder='%$norm_num%' AND body IN ('y','yes','Y','Yes','yea','Yea') AND timestamp BETWEEN ".date('Ymd H:i:s', strtotime($starttime))." AND NOW()"); 
0
source

Source: https://habr.com/ru/post/905554/


All Articles