【问题标题】:How to find specific number in a string php?如何在字符串php中查找特定数字?
【发布时间】:2019-06-20 15:45:45
【问题描述】:

我有不同的字符串,我想在其中找到特定的数字。如果数字在字符串中,它应该打印该数字。

字符串如下:

string(9) "path_12.0" 
string(9) "path_12.1" 
string(9) "path_13.0" 
string(9) "path_13.1" 
string(9) "path_13.2"

数字是这样的:

int(12) 
int(12) 
int(13) 
int(13) 
int(13)

我尝试的是:

if (strpos(','.$mainString.',' , ','.$QuestionId.',') != FALSE) { 
        echo $QuestionId;
} // this doesn't print anything in the body

我也尝试了下面的技巧,但它也没有打印任何东西

if(in_array($QuestionId, explode(',', $mainString))) {
    echo $QuestionId;
}

我想检查这样的事情:

if($questionId is in $mainString) { 
      echo $questionId;
}

注意:我在 StackOverflow 上搜索了类似的问题,但没有找到解决我问题的解决方案,因此我发布了这个问题。

【问题讨论】:

  • 所以你想检查第一个和第二个字符串中是否有 12,最后三个字符串中是否有 13?我不确定我是否理解。
  • @Qirel 我正在遍历所有字符串,所以我肯定想检查所有字符串中的questionId
  • 举个例子,strpos(','.$mainString.',' , ','.$QuestionId.',') 将提供strpos(',path_12.0,' , ',12,') 你不会找到它
  • 字符串周围的逗号有什么意义?
  • 就像@Cid 所说,逗号没有用。使用strpos($mainString, $QuestionId.'.') 应该会导致strpos('path_12.0' , '12.')

标签: php arrays string


【解决方案1】:

另一种选择是使用您的字符串创建一个数组,并使用preg_grep 和一个检查小数的第一部分是否等于其中一个数字的模式。

a pattern 的示例,其中数组中的数字用作替代:

_\K(?:12|13)(?=\.\d+)
  • _\K 匹配下划线并忘记匹配的内容
  • (?:非捕获组
    • 12|13 匹配 12 或 13
  • )关闭非捕获组
  • (?=\.\d+) 正向前瞻,断言右边是一个点和 1+ 个数字

例如:

$numbers = [12, 13];
$strings = [
    "path_12.0",
    "path_12.1",
    "path_13.0",
    "path_13.1",
    "path_13.2",
    "path_14.1"
];

$pattern = "/_\K(?:" . implode('|', $numbers) . ")(?=\.\d+)/";
$resullt = preg_grep($pattern, $strings);
print_r($resullt);

结果

Array
(
    [0] => path_12.0
    [1] => path_12.1
    [2] => path_13.0
    [3] => path_13.1
    [4] => path_13.2
)

Php demo

或者,如果您只想打印数字,您可以使用array_reduce 并收集匹配项:

$result = array_reduce($strings, function($carry, $item) use ($pattern){
    if (preg_match($pattern, $item, $matches)){
        $carry[] = $matches[0];

    }
    return $carry;
});

print_r($result);

结果

Array
(
    [0] => 12
    [1] => 12
    [2] => 13
    [3] => 13
    [4] => 13
)

Php demo

【讨论】:

    【解决方案2】:

    下面可以使用sn-p,

    $paths = ["path_12.0", "path_12.1", "path_13.0", "path_13.1", "path_13.2", ];
    $nos = [12, 12, 13, 13, 13, ];
    function strpos_arr($needle,$haystack)
    {
        if (!is_array($haystack)) {
            $haystack = [$haystack];
        }
        foreach ($haystack as $what) {
            if (($pos = strpos($what,(string)$needle)) !== false) {
                return $pos;
            }
    
        }
        return false;
    }
    foreach ($nos as $key => $value) {
        // checking if question id in in path array with str pos
        if(strpos_arr($value,$paths) !== false){
            echo $value."\n";
        }
    }
    
    

    Demo.

    【讨论】:

      【解决方案3】:
      $array_strings =  ["path_12.0", "path_12.1", "path_13.0", "path_13.1", "path_13.2"];
      $array_numbers = [12, 22, 13, 11, 17];
      $results = [];
      
      foreach ($array_strings as $string){
        preg_match_all('!\d+\.*\d*!', $string, $matches);
        foreach ($array_numbers as $number){
            if (in_array($number, $matches[0])){
                   array_push($results, $number);
               }
          }
      }
      
      print_r($results);
      

      结果:Array ( [0] => 12 [1] => 13 )

      注意 1: 数组答案可以有重复值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-06
        • 2021-12-22
        • 1970-01-01
        • 2022-12-02
        • 1970-01-01
        • 2023-01-23
        相关资源
        最近更新 更多