【问题标题】:Determine if a string contains a sequence of numbers判断一个字符串是否包含一个数字序列
【发布时间】:2016-05-31 08:39:05
【问题描述】:

假设我有一个整数88123401,我想确定它是否包含一个数字序列,如 1234、23456、456789 或类似的任何长度和任何数字开头的数字......这是否可能在 PHP 中,如果是这样,如何找出答案?

【问题讨论】:

  • 你可以使用regular expressions
  • 这听起来像是一个家庭作业问题,你能告诉我们你的尝试吗?
  • 我已经尝试查看子字符串和正则表达式,但我不确定如何确定这个确切的问题。
  • @SahilM 不是作业问题。试图开发一个可以确定电话号码值的函数。 :)
  • 我认为最好的方法是遍历字符串中的所有字符,然后有一个内部 while 来检查从该字符开始的序列。

标签: php integer substring sequence


【解决方案1】:

一些带有 for 的函数,因此您可以遍历所有字符串,将每个字符与其前一个字符进行比较。

function doesStringContainChain($str, $n_chained_expected)
{
    $chained = 1;

    for($i=1; $i<strlen($str); $i++)
    {
        if($str[$i] == ($str[$i-1] + 1))
        {
            $chained++;
            if($chained >= $n_chained_expected)
                return true;
        }else{
            $chained = 1;
        }
    }
    return false;
}

doesStringContainChain("6245679",4); //true
doesStringContainChain("6245679",5); //false

【讨论】:

    【解决方案2】:

    使用循环并使用@jtheman 的答案

    $mystring = '88123401';
    $findme   = array(123,2345,34567);
    foreach ( $findme as $findspecificnum ) {
        $pos = strpos($mystring, $findme);
    
        if ($pos === false) {
            echo "The sequence '$findme' was not found in the number '$mystring'";
        } else {
            echo "The sequence '$findme' was found in the number '$mystring'";
            echo " and exists at position $pos";
        }
    }
    

    保持简单直接。

    【讨论】:

      【解决方案3】:

      将数字视为字符串并使用strpos() 进行搜索。

      例子:

      $mystring = '88123401';
      $findme   = '1234';
      $pos = strpos($mystring, $findme);
      
      
      if ($pos === false) {
          echo "The sequence '$findme' was not found in the number '$mystring'";
      } else {
          echo "The sequence '$findme' was found in the number '$mystring'";
          echo " and exists at position $pos";
      }
      

      来源:http://php.net/manual/en/function.strpos.php

      【讨论】:

      • 会有更有效的方法来处理所有不同的序列吗? 123234534567 等?
      • 您也许可以使用聪明的正则表达式,但我没有足够的资格对此给出最佳答案。
      【解决方案4】:

      这可能会对您有所帮助:

      $number = "88123401";
      
      $splittedNumbers = str_split($number);
      $continuous = false;
      $matches[0] = '';
      $i = 0;
      
      do {
          if ((int)(current($splittedNumbers) + 1) === (int)next($splittedNumbers)) {
              if($continuous) {
                  $matches[$i] .= current($splittedNumbers);
              }
              else {
                  $matches[$i] .= prev($splittedNumbers) . next($splittedNumbers);
                  $continuous = true;
              }
          } else {
              $continuous = false;        
              $matches[++$i] = '';
          }
          prev($splittedNumbers);
      } while (!(next($splittedNumbers) === false));
      
      print_r(array_values(array_filter($matches)));
      

      这会列出数组中所有连续的匹配项。我们可以根据结果进一步处理。

      结果:

      Array
      (
          [0] => 1234
          [1] => 01
      )
      

      【讨论】:

        猜你喜欢
        • 2014-06-18
        • 1970-01-01
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-10
        相关资源
        最近更新 更多