什么是preg_match条件以确保第一个数字是数字7


what is the preg_match condition to ensure the first digit is a number 7?

例如

if(preg_match("/[^0-9]/",$number_from)){
   $error_message .= 'Please Check and try again.<br />';
} 

只确保为字符串输入数字。

但是,确保第一个数字只能是数字"7"的条件是什么,否则会显示上述错误消息?

对不起,我是PHP新手。

注释中建议的非常简单的正则表达式:

/^7'd+$/

将匹配每个只包含数字的字符串,并且以7开头,如果我理解你的问题,这就是你的验证字符串。

要检查错误,可以使用以下语法:

if( ! preg_match( "/^7'd+$/", $number_from ) )
{
   $error_message .= 'Please Check and try again.<br />';
} 

preg_match前面的感叹号!是否定。意思是:"如果不匹配"。