确定字符串是否包含数字序列
Determine if a string contains a sequence of numbers
假设我有一个整数 88123401
,我想确定它是否包含一个数字序列,例如 1234、23456、456789 等任意长度的数字序列。是这在 PHP 中是完全可能的,如果是这样,人们将如何找出答案?
将数字视为字符串并使用 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";
}
有些函数带有 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
使用循环并使用@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";
}
}
保持简单直接。
这可能对您有帮助:
$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
)
假设我有一个整数 88123401
,我想确定它是否包含一个数字序列,例如 1234、23456、456789 等任意长度的数字序列。是这在 PHP 中是完全可能的,如果是这样,人们将如何找出答案?
将数字视为字符串并使用 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";
}
有些函数带有 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
使用循环并使用@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";
}
}
保持简单直接。
这可能对您有帮助:
$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
)