Hello all.
I have a string that will contain the username, for the security reasons i want it to be checked it if its diferent from the specific charset.
I think it could be done by looping the check 1 character from the username at a time till the end of it, but is there any faster way to do it?
For example:
$charset="abcd1234";
$string1="312";
$string2="5abc";
It should return that $string2 is not contained in the charset.
Thanks in advance.

Dani AI

Generated

Asking whether to check a username character-by-character is common. and were right that pattern matching works, but for a simple ASCII whitelist there are lighter, faster options than looping or invoking a full regular expression engine.

A compact, fast approach for the common alphanumeric case is to use the ctype family and a length check. ctype functions run in C and are very quick. Remember to check length first because ctype tests return false on an empty string.

$min = 3;    // choose min/max for your policy
$max = 15;
$len = strlen($user);

if ($len >= $min && $len <= $max && ctype_alnum($user)) {
    // valid
}

If you need a custom whitelist of characters (not just letters+digits), strspn() is an efficient choice: it tests how many leading characters belong to a set, so a single comparison tells you whether every character is allowed.

$allowed = '...'; // your whitelist characters
if (strspn($user, $allowed) === strlen($user)) {
    // valid
}

If usernames must accept non-ASCII (accented letters, CJK, etc.), byte-oriented functions like ctype_* and strspn() won’t behave as expected. Normalize input (Intl Normalizer) and use Unicode-aware matching. See normalizer_normalize for guidance.

Practical notes: always enforce validation server-side, trim input first, enforce length limits, use a strict whitelist (avoid blacklist rules), normalize case if needed, and escape or parametrize any output/storage to prevent injection. For performance, prefer ctype_* or strspn() for simple checks; use regex only when you need richer rules (complex classes or Unicode properties). For details on the built-ins mentioned see ctype_alnum and strspn.

Recommended Answers

All 4 Replies

Hello all.
I have a string that will contain the username, for the security reasons i want it to be checked it if its diferent from the specific charset.
I think it could be done by looping the check 1 character from the username at a time till the end of it, but is there any faster way to do it?
For example:
$charset="abcd1234";
$string1="312";
$string2="5abc";
It should return that $string2 is not contained in the charset.
Thanks in advance.

http://php.net/preg_match That is all

Regular Expressions is what you're looking for. As ShawnCplus said, preg_match() is the function you'll want to use. You might want to do some reading on regular expressions. I used this tutorial a while ago:
http://www.regular-expressions.info/tutorial.html

The problem is that the charset will not have all possible usernames.
Larger example would be :
$charset="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$username="abb";
$username="0000b";
$username2="_abb";
Two first are valid and the 3rd one isnt.
Err,i think im really dumb, creating a charset and trying to compare latter a latter , when i have [A-Za-z0-9]{4,10} lol =)
Ty for help.

The problem is that the charset will not have all possible usernames.
Larger example would be :
$charset="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$username="abb";
$username="0000b";
$username2="_abb";
Two first are valid and the 3rd one isnt.
Err,i think im really dumb, creating a charset and trying to compare latter a latter , when i have [A-Za-z0-9]{4,10} lol =)
Ty for help.

You're welcome =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.