识别字符串php中出现的所有16位数字


Identify all occurrences of a 16 digit number within the string - php

如果我有一个字符串,例如"Hello,my customer number is x and I need to log a fault",其中x始终是16位数字。我需要识别字符串中出现的所有16位数字。

最好的方法是什么?

您可以使用形式为[0-9]{16}(一位数字,16次)的正则表达式。例如:

$message = "blah blah 1024590293404182 blah";
$numbers = array();
preg_match('/[0-9]{16}/', $message, $numbers);

在此之后,$numbers是一个具有来自$message的所有16位数字的数组。你也可以做:

$message = "blah blah 1024590293404182 blah";
$numbers = array();
if (preg_match('/[0-9]{16}/', $message, $numbers)) {
    // There is a 16-digit number in $message, and all numbers are in $numbers
} else {
    // No 16-digit number was found
}

请参阅http://php.net/manual/en/function.preg-match.php.

if (preg_match('/[''d]{16}/', $subject)) {
} else {
}

试试这个代码。它应该在字符串中找到16位数字。