5 7 Votes

PHP: Permit only certain letters, numbers and characters in a string

Tip by Stefan Trost | Last Update on 09/12/2018 | Created on 19/01/2013

Today, I would like to show you a way, how you can check a string for certain letters, numbers or characters. An area of application would be, for example, to check user names which may contain only certain characters.

First, we would like to have a look at the following code:

if (!preg_match("#^[a-zA-Z0-9]+$#", $text)) {
   echo 'String also contains other characters.';	
} else {
   echo 'String only contains numbers and letters.';
}

Here we want to check whether the string $text only consists of letters (a to z and A to Z) and numbers (digits 0 to 9). For this, we use a regular expression and preg_match. As a first parameter, we pass the regular expression, where the actual regular expression is written between "# and #".

The symbol ^ represents the beginning of the string. The $ stands for the end of the string. In between, all characters are allowed, which are defined in the character class between the square brackets. The characters from this class can occur as often as they want. This is indicated by the + sign at the end. Within the character class, we have defined the lower case letters a-z, the upper case letters A-Z and the digits 0-9.

Also check for other Characters like accents

Naturally, accents are not included in the character class a-z or A-Z. If we want to allow these characters, we have to write them down separately:

if (!preg_match("#^[a-zA-Z0-9äöüÄÖÜ]+$#", $text)) {
   echo 'String also contains other characters';	
} else {
   echo 'String only contains letters and digits.';
}

So, we can add any other character to our character class in order to check on these characters, too.

Check for Letters in general

Up to now, we have defined character sets by writing each letter explicitly to our character class. But what can we do if we also want to allow each other letter possible such as è, ø, é or ă? Finally, it is not possible to write all conceivable letters to our regular expression. The following example shows a way of how we can handle this situation.

$text = "abcABCäöüÄÖÜßéèâø";
 
if (preg_match("#^\p{L}+$#u", $text)) {
   echo 'String contains arbitrary letters.'; 
}

L stands for an arbitrary letter, \p for a character that has the property defined in the curly brackets behind and u stands for Unicode. So, this regular expression is checking for arbitrary, arbitrarily repeated Unicode characters. This RegEx can be used since PHP version 5.1.0.

Using L, we are allowing lowercase as well as uppercase written letters. If you only want to check for lowercase or uppercase letters, you can use Ll (Letters lowercase) or Lu (Letters uppercase) instead of L.

Allow spaces

Adding characters to the set even applies to spaces. In this example we have added a space to the character class:

if (!preg_match("#^[a-zA-Z0-9äöüÄÖÜ ]+$#", $text)) {
   echo 'String also contains other characters.';	
} else {
   echo 'String only contains letters, digits and spaces.';
}

With this, also strings containing blanks are allowed.

Points, brackets and other special characters

Some characters have a special meaning within a regular expression. These include, for example, points or brackets. If we would like to include such characters in our character class, we have to write this:

if (!preg_match("#^[a-zA-Z0-9äöüÄÖÜ \.\]]+$#", $text)) {
   echo 'String also contains other characters.';	
}

With \ we can escape the respective characters. That means: with \ we say, that the next character should be understood as a character and not as a regular instruction. In the example, with this, we add a point and a square bracket to our character class. Interesting at the end: The last bracket is the end of our character class, the bracket before has a \ infront and therefore is part of the character class.

But what if we also want to add the \ as a character to our class? It's simple. Also, the \ character can be escaped with writing a \ infront of it. Simply write \\.

CType-Functions

For certain, often used character classes, you can easily use the CType Functions of PHP. More on this topic in my CType string tutorial. The advantage of the CType functions are that we do not need a regular expression and they have a better performance.

ReplyPositiveNegative

About the Author

Avatar AuthorYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile | Message

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.