开发者

Validate that string contains only numbers, letters, and underscores

开发者 https://www.devze.com 2023-01-01 08:37 出处:网络
How do you check if a string contains invalid characters? I want to restrict each user\'s username with PHP to having numbers, letters, and under开发者_StackOverflowscores.You can use a regular expres

How do you check if a string contains invalid characters?

I want to restrict each user's username with PHP to having numbers, letters, and under开发者_StackOverflowscores.


You can use a regular expression:

if (preg_match("^[0-9A-Za-z_]+$", username) == 0) {
    echo "<p>Invalid username</p>";
}


Try:

<?php
function IsSafe($string)
{
    if(preg_match('/[^a-zA-Z0-9_]/', $string) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号