Ignoring phone number if 1 is at the front

53 Views Asked by At

I am trying to send out a text message to phone numbers. It works as long as it is under 11 digits, but if the phone number has 11 digits it fails. Is there a way to be flexible for both?

$phonenum = "16783293991"; //works if 6783293991
  $from_name = "Gregory Smith";
    $from_email = "[email protected]";
    $from = sprintf ("From: %s <%s>\nSender: %s <%s>\nContent-Type: text/plain; charset=\"UTF-8\"\nContent-Transfer-Encoding: 8bit\n", $from_name, $from_email, $from_name, $from_email);
  $to = $phonenum;
$formatted_number = $to."@messaging.sprintpcs.com";
mail("$formatted_number", "", "$message", $from); //the empty field of "" is the subject line and is unnecessary
1

There are 1 best solutions below

0
On BEST ANSWER

Check for 1 as the first digit, and verify length:

if ((substr($phone, 0 , 1) == '1') && (strlen($phone) <= 11)) {
   sms stuff here
}