How do I use PHP's preg_match to validate strings?
25,902
Solution 1
Simple example.. Verifying $var which is a string verifying and checking for (a to z AND A to Z) characters.
<?php
$var = 'hello';
if (ereg("[a-zA-Z]", $var)) {
echo 'it was typed correctly';
} else {
echo 'it was not typed correctly';
}
?>
more regular expressions syntax exemples: http://www.regexlib.com/
EDIT:
if (ereg("^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$", $email)) {
echo 'email ok';
} else {
echo 'email not ok';
}
Regards.
Solution 2
Jeff Atwood recently had an article on his coding horror blog about regular expressions. Check out "Regular Expressions for Regular Programmers".
Author by
OrangeRind
What about me? Nothing really. Regular guy. Bit of brain. Lots of interests.
Updated on July 09, 2022Comments
-
OrangeRind almost 2 years
Please tell me how to use the various symbols used for expression matching in
preg_match
function of PHP.Besides basic information, please give example for checking a valid e-mail address and a string that should not contain / : ; { } * &