如何在php字符串中查找特定的数字?

如何在php字符串中查找特定的数字?,php,arrays,string,Php,Arrays,String,我有不同的字符串,我想在其中找到具体的数字。如果数字在字符串内,则应打印该数字 字符串如下所示: 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.'

我有不同的字符串,我想在其中找到具体的数字。如果数字在字符串内,则应打印该数字

字符串如下所示:

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上搜索了类似的问题,但没有找到解决问题的解决方案,因此我发布了这个问题。

您可以使用下面的代码片段

$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";
    }
}


.

您可以使用下面的代码段

$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";
    }
}

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


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

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



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

另一个选项可以是使用字符串创建一个数组,并与检查小数点的第一部分是否等于其中一个数字的模式一起使用

将数组中的数字用作替换的示例:

_\K(?:12|13)(?=\.\d+)
  • \uk
    匹配下划线并忘记匹配的内容
  • (?:
    非捕获组
    • 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
)
Array
(
    [0] => 12
    [1] => 12
    [2] => 13
    [3] => 13
    [4] => 13
)

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

$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] => path_12.0
    [1] => path_12.1
    [2] => path_13.0
    [3] => path_13.1
    [4] => path_13.2
)
Array
(
    [0] => 12
    [1] => 12
    [2] => 13
    [3] => 13
    [4] => 13
)

另一个选项是用字符串创建一个数组,并与检查小数点的前半部分是否等于其中一个数字的模式一起使用

将数组中的数字用作替换的示例:

_\K(?:12|13)(?=\.\d+)
  • \uk
    匹配下划线并忘记匹配的内容
  • (?:
    非捕获组
    • 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
)
Array
(
    [0] => 12
    [1] => 12
    [2] => 13
    [3] => 13
    [4] => 13
)

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

$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] => path_12.0
    [1] => path_12.1
    [2] => path_13.0
    [3] => path_13.1
    [4] => path_13.2
)
Array
(
    [0] => 12
    [1] => 12
    [2] => 13
    [3] => 13
    [4] => 13
)

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