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}
1
2ive never used regex expressions till now and had loads of difficulty trying to convert a [url]link here[/url] into an href for use with posting messages on a forum, heres what i manage to come up with:
3
4$patterns = array(
5 "/\[link\](.*?)\[\/link\]/",
6 "/\[url\](.*?)\[\/url\]/",
7 "/\[img\](.*?)\[\/img\]/",
8 "/\[b\](.*?)\[\/b\]/",
9 "/\[u\](.*?)\[\/u\]/",
10 "/\[i\](.*?)\[\/i\]/"
11 );
12 $replacements = array(
13 "<a href=\"\\1\">\\1</a>",
14 "<a href=\"\\1\">\\1</a>",
15 "<img src=\"\\1\">",
16 "<b>\\1</b>",
17 "<u>\\1</u>",
18 "<i>\\1</i>"
19
20 );
21 $newText = preg_replace($patterns,$replacements, $text);
22
23at first it would collect ALL the tags into one link/bold/whatever, until i added the "?" i still dont fully understand it... but it works :)
24