我被困在这里,我的问题找不到任何结果,也许是因为英语不是我的母语.
我想匹配一个序列中包含最多30个字母/数字的行:
这是否可以使用preg_match?
preg_match("/[^A-Za-z0-9](max 30 in a sequence)/", $string)
字符串:
$string = "1234567890123456789012345678901234567890"; // FALSE
$string = "sdfihsgbfsadiousdghiug"; // TRUE
$string = "cfgvsdfsdf786sdf78s9d8g7stdg87stdg78tsd7g0tsd9g7t"; // FALSE
$string = "65656.sdfsdf.sdfsdf"; // TRUE
$string = "ewrwet_t876534875634875687te8---7r9w358wt3587tw3587"; // TRUE
$string = "sd879dtg87dftg87dftg87ftg87tfg087tfgtdf8g7tdf87gt8t___454"; // FALSE
解决方法:
您可能想要查找这些字符中是否有30个或更多:
preg_match("/[A-Za-z0-9]{30,}/", $string)
查看粗体匹配:
1234567890123456789012345678901234567890
sdfihsgbfsadiousdghiug
cfgvsdfsdf786sdf78s9d8g7stdg87stdg78tsd7g0tsd9g7t
65656.sdfsdf.sdfsdf
ewrwet_t876534875634875687te8—7r9w358wt3587tw3587
sd879dtg87dftg87dftg87ftg87tfg087tfgtdf8g7tdf87gt8t___454
然后否定结果:
preg_match("/[A-Za-z0-9]{30,}/", $string) === 0
// returns 0 if no match
// or FALSE if error
标签:php,string,match,preg-match,sequence
来源: https://codeday.me/bug/20190725/1528154.html