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 OverflowIf 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.
if(preg_match("/[A-Za-z0-9]+/", $content) == TRUE){
} else {
}
regex - php preg_match only numbers, letters and dot - Stack Overflow
preg_match for alphanumeric - PHP - SitePoint Forums | Web Development & Design Community
regex - php only allow letters, numbers, spaces and specific symbols using pregmatch - Stack Overflow
PHP preg_match regex to find letters numbers and spaces? - Stack Overflow
Use
preg_match('/^[a-z0-9 .\-]+$/i', $firstname)
If you not only want to allow ASCII, then use Unicode properties:
preg_match('/^[\p{L}\p{N} .-]+$/u', $firstname)
\p{L} is any letter in any language, matches also Chinese, Hebrew, Arabic, ... characters.
\p{N} any kind of numeric character (means also e.g. roman numerals)
if you want to limit to digits, then use \p{Nd}
You can use the following regular expression:
^[\w ]+$
This matches any combinations of word characters \w and spaces , but as the guys said be careful because some names might contain other symbols.
So you can use it like this:
if (preg_match("/^[\\w ]+$/", $name)) {
// valid name
}
else {
// invalid name
}
Try this:
<?php
$user_input = 'User_name';
if (!preg_match('/^[a-z0-9_\s]+$/i', $user_input)) {
// Matches English letters, numbers underscores(_) and spaces
$mess = $mess . "Your name must contain letters only.<br>";
$status = "NOTOK";
}
?>
It seems the simplest way is just to do it in two regex's.
if (preg_match('/[A-Za-z]/', $myString) && preg_match('/[0-9]/', $myString))
{
echo 'Contains at least one letter and one number';
}
I suppose another way to do it is this below. It says "a letter and then later on at some point a number (or vice versa)". But the one above is easier to read IMO.
if (preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $myString))
{
echo 'Contains at least one letter and one number';
}
if (preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $myString))
{
echo 'Secure enough';
}
Answer updated based on https://stackoverflow.com/a/9336130/315550, thnx to https://stackoverflow.com/users/116286/jb
simplest to understand, longest to write
[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][A-Z][A-Z]
shorter:
[A-Z]{2}[0-9]{5}[A-Z]{2}
complete lines only:
^[A-Z]{2}[0-9]{5}[A-Z]{2}$ // ^ - start of line; $ - end of line
capture in parens:
(^[A-Z]{2}[0-9]{5}[A-Z]{2}$) // as per mpaepper, for reuse in matched expression
shorter still:
(^[A-Z]{2}\d{5}[A-Z]{2}$) // \d represents 0-9
Then try this:
(^[A-Z]{2}[0-9]{5}[A-Z]{2}$)
Code:
if(preg_match('/[^a-z_\-0-9]/i', $string))
{
echo "not valid string";
}
Explanation:
- [] => character class definition
- ^ => negate the class
- a-z => chars from 'a' to 'z'
- _ => underscore
- - => hyphen '-' (You need to escape it)
- 0-9 => numbers (from zero to nine)
The 'i' modifier at the end of the regex is for 'case-insensitive' if you don't put that you will need to add the upper case characters in the code before by doing A-Z
if(!preg_match('/^[\w-]+$/', $string1)) {
echo "String 1 not acceptable acceptable";
// String2 acceptable
}
PHP is trying to render the variable $s in the double quoted string. Use single quotes instead and you can remove the {1} inside your regular expression because it is not necessary.
$word = '$1$s';
if (preg_match('/^\$[1-9]\$s$/', $word)) {
echo 'true';
} else {
echo 'false';
}
You can also escape the $ in your double quoted string:
$word = "$1\$s";
// Note $1 doesn't need to be escaped since variables can't start with numbers
Finally, you can see why this wasn't working by seeing what your $word actually equaled (with error reporting enabled):
$word = "$1$s"; // Notice: Undefined variable: s on line #
echo $word; // $1
Try this:
if(preg_match('/\$[1-9]{1}\$s/',$word)){
echo 'true';
} else {
echo 'false';
}
That would be:
if(preg_match('/[^a-z\s-]/i',$stringabc))
for "anything but letters (a-z), spaces (\s, meaning any kind of whitespace), and dashes (-)".
To also allow numbers:
if(preg_match('/[^0-9a-z\s-]/i',$stringabc))
You can use something like:
preg_match("/[^a-z0-9 -]/i", $stringabc)