我想在使用之前使用正则表达式验证我的数字,但是它没有成功。
我希望第一个数字以0开头,接着是2或5,然后是4,其余的数字继续到10位数。
<?php
$str = "0241310102";
$pattern = "([0]{1}[2,5]{1}[4]{1}[0-9]{7})";
if(preg_match($pattern, $str)){
echo "YES";
}else{
echo "NO";
}
?> Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
^,$)。[0]→00{1}→0[2,5]将包括字符类中的逗号→[25]$examples = [ "0241310102", "1241310102", "0541310102", "0551310102", "0241" ]; $pattern = '(^0[25]4[0-9]{7}$)'; foreach ($examples as $example) { echo $example, ": "; if (preg_match($pattern, $example)) { echo "YES", "\n"; } else { echo "NO", "\n"; } }