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 帐户的长度。
您的用户名最多可包含 15 个字符。
function validate_username($username) { return preg_match('/^[A-Za-z0-9_]{1,15}$/', $username); }
如何更改此规则以使字符串开头的 @ 符号可选!
有效输入样本
abcdabcdabcdabc
@abcdabcdabcdabc
可?用于使前一个字符可选:
?
preg_match('/^@?[A-Za-z0-9_]{1,15}$/', $username);
等效地:
preg_match('/^@{0,1}[A-Za-z0-9_]{1,15}$/', $username);