1<?php
2// First Verif your regex code with https://regex101.com/
3$str = "Visit W3Schools";
4$pattern = "/w3schools/i";
5echo preg_match($pattern, $str); // Outputs 1
6
7// test email with REGEX
8if (!preg_match("/[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+.[a-zA-Z]{2,4}/", $emailAddress)){
9 //Email address is invalid.
10}
11
12// use filter var to valide Email
13if(filter_var($emailAddress, FILTER_VALIDATE_EMAIL))
14{
15 //The email address is valid.
16} else{
17 //The email address is invalid.
18}
19
20
21?>
22
1<?php
2//Syntex : int preg_match( $pattern, $input, $matches, $flags, $offset)
3
4// Declare a variable and initialize it
5$str = "Check For Testing.";
6
7// case-Insensitive search for the word "Check"
8if (preg_match("/\bCheck\b/i", $str, $match))
9 echo "Matched!";
10else
11 echo "not matched";
12
13
14// Output : Matched
15?>
1if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
2 echo "A match was found.";
3} else {
4 echo "A match was not found.";
5}
1if(!preg_match('/^\[a-zA-Z]+$/',$input)) {
2 // String contains not allowed characters ...
3}
1preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) : int