简体   繁体   English

PHP正则表达式数字加一个字母

[英]PHP regular expression numeric plus one letter

I'm trying to achieve validation for numeric input plus letter X or x. 我正在尝试验证数字输入加字母X或x。 So far I have: 到目前为止,我有:

if(preg_match('/[^0-9xX]+$/', $data)) return 'error';

It correctly returns error for all cases EXCEPT a123. 对于除a123以外的所有情况,它都会正确返回错误。 Have anyone idea why? 有谁知道为什么?

Use ^ at the start of regex to check if the complete string instead of sub-string. 在正则表达式的开头使用^检查是否完整的字符串而不是子字符串。

if(preg_match('/^[0-9xX]+$/', $data))
    return 'error';

Try this: 尝试这个:

if(preg_match('/^[0-9x]+$/i', $data)){
 echo 'match';
}else{
 echo 'no match';
}

Its like yours, only the other way round. 就像您一样,只是相反。 ^ is for the begin of the string. ^是字符串的开头。 $ For the end of the string. $用于字符串的结尾。 So the whole string must match the pattern. 因此,整个字符串必须与模式匹配。

if(!preg_match('/^([0-9]+|[xX])$/', $data)) return 'error';

Maybe this would be better, it match the right case,and add ! 也许这会更好,它匹配正确的情况,然后添加! before match result. 比赛结果之前。

I don't got it. 我不知道

if(preg_match('/^[0-9xX]+$/', $data)) return 'error';

still doesn't return error when we enter 123. 当我们输入123时,仍然不会返回错误。

I'm sorry if i make misunderstanding. 对不起,我很抱歉。

if(preg_match('/[0-9]xX$/', $data)) return 'error';

All number will be followed by xX. 所有数字后面都将是xX。 CMIIW. CMIIW。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM