如何避免php中仅在名称验证中使用数字?我想做名称允许空格,点,特殊字符,字符串但不允许只有数字如何?
我使用以下代码,
array('first_name,last_name', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message' => '{attribute} contain Alphabets only',),
它也不允许特殊字符?
<?php
$string='a123';
if(preg_match('/[0-9]/',$string)){
echo 'you are not allowed to use numbers';
}else{
//proceed
}
?>
试试这个,这将允许空格、点、特殊字符、字符串,但不允许只使用数字。你想要的。
<?php
$words = 'string@1323_';
$words = preg_replace('/'d+/', '', $words );
echo $words;
?>
如果您正在寻找一种方法来验证一个只允许使用字母、空格、句点和-
的名称,那么我会使用以下方法:
if (preg_match('/^['p{L} '.'-]+$/u', $name)) {
// $name is valid
}
我使用了Unicode字符属性'p{L}
,它代表Unicode字母。
数字是不允许的,因为它们没有列在允许的字符集中。