【问题标题】:php regular expression minimum and maximum length doesn't work as expectedphp正则表达式最小和最大长度不能按预期工作
【发布时间】:2014-04-28 23:45:57
【问题描述】:

我想在 PHP 中创建一个正则表达式,它允许用户输入以下任一格式的电话号码。

345-234 898

345 234-898

235-123-456

548 812 346

数字的最小长度应为 7,最大长度应为 12。

问题在于,正则表达式不关心最小和最大长度。我不知道它有什么问题。请帮我解决它。这是正则表达式。

if (preg_match("/^([0-9]+((\s?|-?)[0-9]+)*){7,12}$/", $string)) {
echo "ok";
} else {
echo "not ok";
}

感谢您阅读我的问题。我会等待回复。

【问题讨论】:

  • 如果电话号码有 12 位数字,它的外观如何? 7?
  • 这个怎么样:$patten = "/\d{3}.\d{3}.\d{3}/";
  • +1 800 123 4567(800) 123 456718001234567 等呢?有很多方法可以写出有效的电话号码。最好简单地从用户输入字符串中提取数字并从中进行验证。

标签: php regex max minimum


【解决方案1】:

只需这样做:

if (preg_match("/^\d{3}[ -]\d{3}[ -]\d{3}$/", $string)) {

这里的\d 表示来自0-9 的任何数字。 [ -] 也表示空格或连字符

【讨论】:

    【解决方案2】:

    您可以在模式开头使用前瞻断言(?=...) 检查长度:

    /^(?=.{7,12}$)[0-9]+(?:[\s-]?[0-9]+)*$/
    

    【讨论】:

      【解决方案3】:

      分解您的原始正则表达式,它可以如下所示:

      ^                   # start of input
      (
          [0-9]+          # any number, 1 or more times
          (
              (\s?|-?)    # a space, or a dash.. maybe
              [0-9]+      # any number, 1 or more times
          )*              # repeat group 0 or more times
      )
      {7,12}              # repeat full group 7 to 12 times
      $                   # end of input
      

      因此,基本上,您允许“任何数字,1 次或多次”,然后是一组“任何数字 1 次或多次,0 次或多次”重复“7 到 12 次” - 哪种杀死你的长度检查。

      您可以采取更严格的方法并写出每个单独的数字块:

      (
          \d{3}         # any 3 numbers
          (?:[ ]+|-)?   # any (optional) spaces or a hyphen
          \d{3}         # any 3 numbers
          (?:[ ]+|-)?   # any (optional) spaces or a hyphen
          \d{3}         # any 3 numbers
      )
      

      简化:

      if (preg_match('/^(\d{3}(?:[ ]+|-)?\d{3}(?:[ ]+|-)?\d{3})$/', $string)) {
      

      如果您想将分隔符限制为只有一个空格连字符,您可以更新正则表达式以使用[ -] 而不是(?:[ ]+|-);如果您希望它是“可选的”(即数字组之间不能有分隔符),请在每个末尾添加 ?

      if (preg_match('/^(\d{3}[ -]\d{3}[ -]\d{3})$/', $string)) {
      

      【讨论】:

        【解决方案4】:

        您可以使用 preg_replace 去除非数字符号并检查结果字符串的长度。

        $onlyDigits = preg_replace('/\\D/', '', $string);
        $length = strlen($onlyDigits);
        if ($length < 7 OR $length > 12)
          echo "not ok";
        else
          echo "ok";
        

        【讨论】:

        • +1 为已发布的最佳解决方案进行验证,尽管我认为您不需要双重转义。 :)
        • 感谢您的回答,这是最好的解决方案,但是,其他答案也很好。
        【解决方案5】:

        您应该在图案上使用开始 (^) 和结束 ($) 符号

        $subject = "123456789";
            $pattern = '/^[0-9]{7,9}$/i';
            if(preg_match($pattern, $subject)){
                echo 'matched';
            }else{
                echo 'not matched';
            }
        

        【讨论】:

          【解决方案6】:

          希望能帮到你。

          Validator::extend('price', function ($attribute, $value, $args) {
          return preg_match('/^\d{0,8}(\.\d{1,2})?$/', $value);
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-03-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多