PHP - Regex to Allow Letters and Numbers Only

PHP - regex to allow letters and numbers only

1. Use PHP's inbuilt ctype_alnum

You dont need to use a regex for this, PHP has an inbuilt function ctype_alnum which will do this for you, and execute faster:

<?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";
}
}
?>

2. Alternatively, use a regex

If you desperately want to use a regex, you have a few options.

Firstly:

preg_match('/^[\w]+$/', $string);

\w includes more than alphanumeric (it includes underscore), but includes all
of \d.

Alternatively:

/^[a-zA-Z\d]+$/

Or even just:

/^[^\W_]+$/

PHP regex accepts only letters and numbers and only one dot

Only one dot, but anywhere in the string? I'd make that a second check:

if(preg_match('/[^A-Za-z0-9\.]/', $username) || substr_count($username, '.') > 1){
$error = "Invalid Username";
}

javascript and php regex to allow only letters, numbers, full stops, asterisks, underscores and dashes

You can use

var regex = /^[a-zA-Z0-9-.*_]+$/; // for JS
$test = preg_replace('/[^a-zA-Z0-9-.*_]/', '', $ebay_username); // for PHP

The main idea is to just add the symbols you need to the character class. Also, the $ anchor is preventing from removing all the unwanted characters from the string anchoring the match at string end.

About the hyphen: it is after a range inside a character class and is thus parsed as a literal - symbol. As best practice, it should be put at the end of the character class so as not to have to escape it (though it will not work in ElasticSearch where it still should be escaped at the end of the character class, but not at its start).

PHP regex to match all single letters followed by numeric value in string

You may use this regex in php using preg_match_all:

preg_match_all('/(?<![a-zA-Z])[a-zA-Z]\K\d+/', $string, $matches);

Resulting in array $matches[0] to return all the matches.

RegEx Demo

RegEx Details:

  • (?<![a-zA-Z]): Make sure we don't have a letter before current position
  • [a-zA-Z]: Match a letter
  • \K: Reset match info
  • \d+: Match 1+ digits

php preg_match only numbers, letters and dot

You need to use anchors and a quantifier:

echo preg_match("/^[a-z0-9.]+$/i", $accountname);

Your string +_#luke123* contains a letter and a number, thus there is a match. If we tell the engine to only match the whole string from beginning (^) to end ($), we'll make sure this will not match. + ensures we capture not just 1, but all characters.

See this demo, now there is no match!

EDIT:
Since you also need to check these conditions:

string must start with 2 or more letters or numbers and end with 1 or
more letters or numbers

I can suggest this ^[a-z0-9]{2,}[a-z0-9.]*[a-z0-9]+$ regex (must be used with i option) that means:

  • Starts with 2 or more letters or numbers
  • then follow any number of digits, letters or periods
  • and ends in 1 or more letters or numbers.

Another demo

php only allow letters, numbers, spaces and specific symbols using pregmatch

Use

preg_match('/^[a-z0-9 .\-]+$/i', $firstname)

Regex in php - letters and numbers with limited length

Do:

^(?=.*(?:[A-Za-z].*\d|\d.*[A-Za-z]))[A-Za-z0-9]{10,50}$
  • (?=.*(?:[A-Za-z].*\d|\d.*[A-Za-z])) is zero width positive lookahead, this makes sure there is at least one letter, and one digit present

  • [A-Za-z0-9]{10,50} makes sure the match only contains letters and digits

Demo


Or even cleaner, use two lookaheads instead of OR-ing (thanks to chris85):

^(?=.*[A-Za-z])(?=.*\d)[A-Za-z0-9]{10,50}$

PHP regex for letters numbers and hyphens

Below is the code that will do the trick.

$str = 'foo-bar!';
if (preg_match("/^[A-Za-z0-9-]+$/", $str)) {
// contains only letters, numbers and hyphens
echo 'only numbers letters and hyphens';
} else {
// does not contain only letters, numbers, and hyphens
echo 'fail';
}

php regex to match a single letter after digits

^\d+[a-zA-Z]$

Try this.Your code uses [] which can match any character out of the list provided.Also use anchors to make a strict match and no partial matches.See here



Related Topics



Leave a reply



Submit