【问题标题】:Ignoring phone number if 1 is at the front如果 1 在前面,则忽略电话号码
【发布时间】:2014-11-28 19:22:45
【问题描述】:

我正在尝试向电话号码发送短信。只要它在 11 位以下,它就可以工作,但如果电话号码有 11 位,它就会失败。有没有办法对两者都灵活?

$phonenum = "16783293991"; //works if 6783293991
  $from_name = "Gregory Smith";
    $from_email = "myemail@gmail.com";
    $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

【问题讨论】:

  • if (substr($phonenumber, 0, 1) == '1') { continue; }?
  • 所以,我应该找出第一个数字是否为 1,然后将其删除...我应该检查长度吗?
  • 好吧,如果您只处理北美数字,那么所有数字都可能以 1 开头,因为那是 N.A. 国家代码。但检查长度不会受到伤害。例如sprint 不知道如何处理 8675309@messaging...
  • $ch = strlen ( $phonenum );回声 $ch; if ($ch === "11") { echo 显示它有 11 个字符长,但是这个比较检查失败
  • 因为$ch是整数,===是严格比较操作。键入 AND 值。 '42' === 42 为假,因为它是 string==int,'42' == 42 为真,因为值匹配。执行$ch === 42$ch == '42'

标签: text server phone-number mass-assignment


【解决方案1】:

检查1作为第一个数字,并验证长度:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多