检查手机号码CF7

时间:2018-06-15 09:51:32

标签: php regex

我有一个ContactForm7表单,可以将数据提交给CRM。一切正常,但现在我需要区分移动和固定电话号码。如果号码以07开头,则会被接受为手机号码。

查看其他线程我尝试过以下但现在既没有在crm中填充移动电话或电话字段,也没有传递给日志文件?

 function process_contact_form_data( $contact_form ) {
     $title = $contact_form->title;
     $submission = WPCF7_Submission::get_instance();

     if ( $submission ) {
        $posted_data = $submission->get_posted_data();
     }


    if ( 'Quote Form_Contact' || 'Quote Form_Product' || 'Quote Form'  == $title ) {

      $firstName = $posted_data['user_first_name'];
      $lastName = $posted_data['user_last_name'];
      $email= $posted_data['your-email'];
      $phone = $posted_data['your-number'];
      $message = $posted_data['your-message'];
      $bp = $posted_data['BP'][0];

      $phone = $pattern;    
      $pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
      $match = preg_match($pattern,$phone);
      if ($match != false) {$mobile = $phone;} else {$mobile= '';};
    }

    $error = false;
    try
    {
    $relationshipId =   postRelationship($firstName,$lastName,$email,$phone,$bp);
    $opportunityId = postOpportunity($relationshipId,$message);
    postOpportunityNote($relationshipId,$opportunityId,$message);
      //  postTask($relationshipId);
    }
    catch (Exception $e)
    {
      $error=true;
    }
    if($error || !isset($relationshipId) || !isset($opportunityId) || $relationshipId <= 0 || $opportunityId <= 0)
    {
      $log->lfile(ABSPATH . 'quotevine.log');
      $log->lwrite('ERROR: With Email Address ' . $email);
      $log->lclose();
    }
 }
 add_action( 'wpcf7_before_send_mail', 'process_contact_form_data');

2 个答案:

答案 0 :(得分:0)

您使用未定义的变量覆盖手机。

  // Phone is now a phone number I assume
  $phone = $posted_data['your-number'];
  $message = $posted_data['your-message'];
  $bp = $posted_data['BP'][0];

  // $pattern is as far as I can see undefined
  // $phone =NULL
  $phone = $pattern;    
  // You set pattern
  $pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
  // Here you regex if the pattern matches NULL which it does not.
  $match = preg_match($pattern,$phone);

答案 1 :(得分:0)

您正在使用未定义的变量覆盖此行$phone中的$phone = $pattern;变量,这将导致$phone为NULL。

但在评论该行后,$mobile的值仍然不正确,因为移动电话号码以07开头且regex与固定电话号码和手机号码匹配,例如:

07123123123
+447123123123

您可以做的是如果匹配成功,请检查字符串是否以+44开头,以验证它是否为手机号码。 如果发生错误,preg_match会返回false,但我认为您要验证匹配是否正确。

$phone = "+447123123123";
$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
$mobile= '';

if (preg_match($pattern,$phone) && 0 === strpos($phone, '07')) {
    $mobile = $phone;
}