3.PHP Regular Expression
3.PHP Regular Expression
STRINGS
REGULAR EXPRESSION
PHP STRINGS
<?php
// single-quote strings
$site = ‘WT';
echo ‘welcome to $site’; single-quotes strings in PHP do not process special characters($)
?>
PHP STRINGS
✓ double-quote strings
<?php
$name = "WT";
echo "Course: $name <br>";
echo 'Course: $name';
?>
PHP STRINGS
✓ Heredoc An identifier is given after the heredoc (<<< ) operator, after
which any text can be written as a new line is started. To close
the syntax, the same identifier is given without any tab or space.
<?php
$input = <<<testHeredoc
Strings are defined as an array of characters.
testHeredoc;
echo $input;
?>
PHP STRINGS
✓ Nowdoc
<?php
$input = <<<'testNowdoc'
Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.
testNowdoc;
echo $input;
?>
PHP STRINGS
✓ Heredoc ✓ Nowdoc
<?php <?php
$Message = 'Hello'; $Message = 'Hello';
$str = <<<example $str = <<<‘example’
$Message all !!! $Message all !!!
example; example;
echo $str; echo $str;
?> ?>
PHP STRING FUNCTIONS
• strlen()
• strrev()
• str_replace()
• strpos()
• trim() - remove whitespaces or strings from both sides of a string.
• explode() - string into an array
• strtolower()
• strtoupper()
• strwordcount()
• substr()
PHP REGULAR EXPRESSION
• A regular expression is a sequence of characters that forms a search pattern. When
you search for data in a text, you can use this search pattern to describe what you
are searching for.
• A regular expression can be a single character or a more complicated pattern.
• Regular expressions can be used to perform all types of text search and text
replacement operations.
PHP-SYNTAX
• In PHP, regular expressions are strings composed of delimiters,
a pattern, and optional modifiers.
$exp = "/php/i";
✓ / is the delimiter,
✓ php is the pattern that is being searched for, and
✓ i is a modifier that makes the search case-insensitive.
\ Escape character
PHP-REGULAR EXPRESSION QUANTIFIERS
Quantifiers define quantities
PHP-REGULAR EXPRESSION GROUPING
You can use parentheses ( ) to apply quantifiers to entire patterns.
They also can be used to select parts of the pattern to be used as a match.
<?php
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo preg_match($pattern, $str);
?>
PHP-REGULAR EXPRESSION GROUPING
Regular Expression Matches
hello The string “hello”
^hello The string which starts with “hello”
hello$ The string which has “hello” at the end.
^hello$ The string where “hello” is alone on a string.
[abc] a, b, or c
[a-z] Any lowercase letter
[^A-Z] Any letter which is NOT an uppercase letter
(gif|png) Either “gif” or “png”
[a-z]+ One or more lowercase letters
^[a-zA-Z0-9]{1, }$ Any word with at least one number or one letter
([ax])([by]) ab, ay, xb, xy
[^A-Za-z0-9] Any symbol other than a letter or other than the number
([A-Z]{3}|[0-9]{5}) Matches three letters or five numbers
PHP-FUNCTIONS
PHP-FUNCTIONS(Example)
<?php
$str = "Learn PHP";
$pattern = "/php/i";
echo preg_match($pattern, $str);
?>
PHP-FUNCTIONS(Example)
<?php
$str = "The rain fall rate is less in chennai.";
$pattern = "/ra/i";
echo preg_match_all($pattern, $str);
?>
PHP-FUNCTIONS(Example)
<?php
$regex = "/<b>(.*)<\/b>/U";
$inputString = "Name: <b>John</b> Position:
<b>Developer</b>";
preg_match_all($regex, $inputString, $output);
echo $output[0][0]." <br> ".$output[0][1]."\n";
?>
PHP-FUNCTIONS(Example)
<?php
$regex = "([0-9]+)";
$original = "Completed graduation in 2004";
$replaceWith = "2002";
$original = preg_replace($regex, $replaceWith,
$original);
echo $original;
?>
PHP-FUNCTIONS(Example)
<?php
// Declare a string
$ip = "134.645.478.670";
$regex = "/\./";
// Use preg_split() function to convert a given string into an array
<?php
$my_email = "[email protected]";
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/", $my_email))
{
echo "$my_email is a valid email address";
}
else
{
echo "$my_email is NOT a valid email address";
}
?>