在将数据发送到数据库之前,在php中为手机号码添加前缀

时间:2012-08-02 22:50:09

标签: php

  

可能重复:
  Please help me VALIDATE Mobile no in php form

我有手机号码验证,但我被困在前缀部分。

if(empty($_POST['mobileno'])) // phone number is not empty
{
$error[] = 'Please enter mobile number';
} else {
if(preg_match('/^\d{10}$/',$_POST['mobileno'])) // phone number is valid
{
  $mobile = $_POST['mobileno'];
  // your other code here
}
else // phone number is not valid
{
  $error[] = 'Phone number invalid !';
}
}
$prefix = 0;
$pmobile = $prefix . $mobile;

这就是我要为前缀做的事情。这是正确的,因为我无法在数字之前添加值0.

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用正则表达式?

 //check the first section is singular or multi, check area code between 0 and 9, check any additional integers
 $pattern = "[0|00]+[0-9]+[^\\s]";
 $phone_number = "000263712702879"; //international phone number
 if(preg_match($pattern, $phone_number)) {
      echo "this is a number";
 } else echo "this not a number";