If you just want to ensure a string contains only alphanumeric characters. A-Z, a-z, 0-9 you don't need to use regular expressions.

Use ctype_alnum()

Example from the documentation:

<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
    if (ctype_alnum($testcase)) {
        echo "The string $testcase consists of all letters or digits.\n";
    } else {
        echo "The string $testcase does not consist of all letters or digits.\n";
    }
}
?>

The above example will output:

The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consist of all letters or digits.
Answer from Jacob on Stack Overflow
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 393999 › pregmatch-for-letters-numbers-slash-and-hmm
php - pregmatch for letters, numbers, slash and ... | DaniWeb
The ~ marks the begin and end of the regex (any 2 matching chars). You do not need a comma to separate the items. The + says it has to be one or more of the preceding characters. ... I forgot to mention that it needs to start with a slash / and only contain lower case letters and numbers too and the spacial chars as / _ - :-) if(!preg_match([a-z][0-9][-/_], $url_key)){ $error[1] = '<small>Error submitting url key!</small>'; }
Discussions

regex - php preg_match only numbers, letters and dot - Stack Overflow
I have been searching for 2 hours now and I still don't get it. I need to evaluate the input of the account name. That can ONLY contain numbers (0-9), letters (a-z and A-Z) and the dot (.). Every... More on stackoverflow.com
🌐 stackoverflow.com
preg_match for alphanumeric - PHP - SitePoint Forums | Web Development & Design Community
Hi, I’m trying to test that $pm_user does not contain anything other than letters or numbers a-z A-Z 0-9 I put the followiing code but it doesn’t ever seem to set $pm_good_login to 0 $pm_good_for_login = 1 if (!preg_match(‘/[1]/’,$pm_user)): $pm_good_for_login = 0 ; endif ; TIA Pat ... More on sitepoint.com
🌐 sitepoint.com
0
June 1, 2013
regex - php only allow letters, numbers, spaces and specific symbols using pregmatch - Stack Overflow
on my php i use preg_match to validate input texts. More on stackoverflow.com
🌐 stackoverflow.com
PHP preg_match regex to find letters numbers and spaces? - Stack Overflow
When people sign up to my site I validate their names with this code: if (preg_match("[\W]", $name)) { $mess = $mess . "Your name must contain letters only. "; $status = "NOTOK"; ... More on stackoverflow.com
🌐 stackoverflow.com
October 26, 2013
🌐
Quora
quora.com › Can-you-explain-preg_match-and-how-to-accept-only-letters-for-name-and-numbers-for-contact-number-in-php
Can you explain preg_match() and how to accept only letters for name and numbers for contact number in php? - Quora
Avoid overly strict rules — many real names contain characters beyond ASCII letters. ... Always trim input and consider character normalization (UTF-8). Combine client-side checks (for UX) with server-side preg_match() validation (security). For phone numbers, prefer using a parsing/validation ...
🌐
PrintMyFonts
askingbox.com › tip › php-permit-only-certain-letters-numbers-and-characters-in-a-string
PHP: Permit only certain Letters, Numbers and Characters in a String
August 3, 2026 - An area of application would be, for example, to check user input such as user names which may contain only specific characters or character groups. First, we would like to have a look at the following PHP code: if (preg_match("#^[a-zA-Z0-9]+$#", $text)) { echo 'String only contains numbers ...
🌐
PHP
php.net › manual › en › function.preg-match.php
PHP: preg_match - Manual
There does not seem to be any mention of the PHP version of switches that can be used with regular expressions. preg_match_all('/regular expr/sim',$text). The s i m being the location for and available switches (I know about) The i is to ignore letter cases (this is commonly known - I think) The s tells the code NOT TO stop searching when it encounters \n (line break) - this is important with multi-line entries for example text from an editor that needs search.
🌐
SitePoint
sitepoint.com › php
preg_match for alphanumeric - PHP - SitePoint Forums | Web Development & Design Community
June 1, 2013 - Hi, I’m trying to test that $pm_user does not contain anything other than letters or numbers a-z A-Z 0-9 I put the followiing code but it doesn’t ever seem to set $pm_good_login to 0 $pm_good_for_login = 1 if (!preg_match(‘/[1]/’,$pm_user)): $pm_good_for_login = 0 ; endif ; TIA Pat a-zA-Z0-9 ↩︎
Find elsewhere
🌐
CodeGenes
codegenes.net › blog › php-preg-match-for-only-numbers-and-letters-no-special-characters
PHP preg_match Syntax: Allow Only Numbers and Letters (No Special Characters) for Form Validation — codegenes.net
Validating alphanumeric input with PHP’s preg_match is a foundational security and data integrity practice. By using the regex /^[A-Za-z0-9]+$/, you ensure input contains only letters and numbers, blocking special characters and reducing risks.
🌐
SitePoint
sitepoint.com › php
Preg_match to confirm letters/numbers/underscores only? - PHP - SitePoint Forums | Web Development & Design Community
April 26, 2003 - Hey, I’ve had people register with ^, *, and allt hese other weird characters. I want to confirm on registration that it’s just letters, numbers, underscores, or dashes. How can I do this? Regards, Someonewhois
🌐
PHP Freaks
forums.phpfreaks.com › php coding › regex help
preg_match that checks for lower/upper case letters, numbers, spaces, etc. - Regex Help - PHP Freaks
June 16, 2012 - Can anyone tell me the preg_match argument that will check to make sure a string contains only the following things... [*]Lower/Upper Case Letters [*]Numbers [*]Hyphens [*]Underscores [*]Colons [*]% Signs [*]$ Signs [*]Periods [*]Comas [*]! Marks