Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
以下用于验证 Twitter 用户名的正则表达式函数不起作用,因为 Twitter 名称可以最少 1 个字符,最多 20 个字符。但是,当我对此进行测试时,它允许用户名大于 20 个字符。我哪里做错了?
public function val_username($subject) { return (bool)preg_match('/[a-zA-Z0-9_]{1,20}/', $subject); }
你忘了$和^
$
^
/^[a-zA-Z0-9_]{1,20}$/应该管用
/^[a-zA-Z0-9_]{1,20}$/
public function val_username($subject) { return (bool)preg_match('/^[a-zA-Z0-9_]{1,20}$/', $subject); }