【发布时间】:2023-01-12 20:07:03
【问题描述】:
我有以下字符串示例:034a412f500535454e5
在这里,我会取出 500。
搜索字符串总是前面有 8 位数字,后面有 8 位数字。 “500”可以有不同长度的数字(p.ex. 12345)。
有很多试验结束错误我发现
preg_match('/(.{8})(.*)(.{13})/', $a, $matches);
有用。但我认为事实并非如此。
我不明白为什么左边是{8},右边是{13}。
我在下面得到我的字符串:
$lastInsertedId = 500;
$numArray = str_split(bin2hex(random_bytes(8)), 8);
$newArray = [$numArray[0],$lastInsertedId,$numArray[1]];
$a = vsprintf('%s%s%s',$newArray).RT;
通过使用:
preg_match('/^.{8}\K.*(?=.{8}$)/', $a, $matches);
结果是 50053545。它不会返回正确的值。
通过使用:
preg_match('/^.{8}\K.*(?=.{8}$)/', '034a412f500535454e5', $matches);
它给了 500 回
怎么了?
gettype($a) 返回字符串。 我在 php 8.1.13
【问题讨论】:
标签: php regex preg-match