Jump to Navigation

022 - Regular Expression

Reqular Expression can:

  • Replace text
  • Test for a pattern within a string
  • Extract a substring from within a string

PHP contains two ways to perform regular expressions, known as POSIX-extented and Perl-Compatible
Regular expressions(PCRE). The PCRE functions are more powerful than the POSIX ones, and faster too,
You should use the PCRE function.

POSIX: ereg()

$username = "jasoN";
if (ereg("([^a-z])", $username)) {
echo "Username must be all lowercase!";
}else{
echo "Username is all lowercase";
}

PCRE: preg_match()

$username = "jasoN";

if (preg_match("/[a-z]/", $username)) {
echo "Username must be all lowercase!";
}else{
echo "Username is all lowercase";
}



Main menu 2

Story | by Dr. Radut