查找具有固定起始数字的数字

Find number with fixed start digits

我正在尝试打印一行,当我在该行中找到以 0123 开头的 10 或 11 位数字时。

$line 将包含大量数据,我感兴趣的只是检查该行并查看是否有以 0123 开头且长度为 10 或 11 位的数字。我不在乎它在哪一行。

我当前的代码如下所示:

$lines = file('test.txt');

foreach ($lines as $line) {
    if (strpos($line,'0123?????'))
        echo "$line<br/>";
}

我发现 strpos() 不支持通配符。那么有没有办法解决这个问题 strpos() 接受通配符或者我需要以其他方式做到这一点?

https://3v4l.org/tLH7B

foreach($lines as $line){
    $len = strlen($line);
    if(($len==10||$len==11)&&"0123"===substr($line, 0, 4))
        echo "valid: $line \n";
}

你需要通配符做什么?

我认为有一些功能可以连续执行,但是老派:

1.检查字符串是否有前缀 0123:

substr($line, 0, 4 ) === "0123"

2。获取字符串计数:

strlen($line) >= 10

等等:

$lines = file('test.txt');
foreach ($lines as $line) {
    $stringCount = strlen($line);
    if (substr($line, 0, 4 ) === "0123" && ($stringCount == 10 || $stringCount == 11))
    echo "$line<br/>";
}

编辑:

$pos = strpos($lines, '0123');

if ($pos) {
    $diff = $stringCount - $pos
    if($diff == 10 || $diff == 11) {
        echo "$line<br/>";
    }
}