Regular Expressions

0 evangelinestamb December 12, 2020

A regular expression is the sequence of characters that helps us to find a particular word or sentence from the sentence or paragraph. Regular expressions may be of a single word or of a complicated one.

We can use regular express to find or replace particular word or sentence from the sentence or paragraph.

Syntax

In PHP, Regular expression contains delimiters, a pattern and optional modifiers as shown below.

$website = "/codehelppro/i";

In the above example, / is the delimiters and codehelppro is the search patterns and i is the modifier which is optional.

Regular Expression Functions

In PHP, we have many in-built functions that helps us to match the patterns or regular expressions.

FunctionUse
preg_match()Match Reguar Expression
preg_match_all()Match global Regular Expression
preg_replace()Find and Replace using Regular Expression
preg_grep()Return array that match the regular expression
preg_split()Split that match the regular expression
preg_quote()Quote matched character from string

Note: preg_match() function stop searching after finding first match but preg_match_all() result all the match output.

1. preg_match() function

preg_match() function will check whether the given regular expression exist on string or not. Let’s look one example to make clear concepts.

<?php

$string = "Welcome to Google";
echo preg_match("/google/i", $string);
//Output : 1

?>

2. preg_match_all() Function

preg_match_all() function will result all the match number using regular expression. Let’s look one example to make clear concepts.

<?php

$str = "Welcome to Code Help Pro to learn Programing.";
$pattern = "/pro/i";
echo preg_match_all($pattern, $str); 
// Output : 2

?>

3. preg_replace() Function

preg_replace() function replaces the match string using a regular expression. Let’s look at one example to make clear concepts.

<?php

$string = "Visit Google!";
$pattern = "/google/i";
echo preg_replace($pattern, "Code Help Pro", $string); 
// Output : "Visit Code Help Pro!"

?>

4. preg_grep() Function

preg_grep() function return array entries that match given patten or regular expression. Let’s look at one example to make clear concepts.

<?php

$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);

?>

The above program return all the array elements conatining floating point numbers.

5. preg_split() Function

preg_split() function split the string using pattern or regular expressions. Let’s look at one example to make clear concepts.

<?php

$keywords = preg_split("/[\s,]+/", "Thanks, Code Help Pro");
print_r($keywords);

?>

The above program split the string by spaces or commas or line break . The output of the above program is:

Array
(
    [0] => Thanks
    [1] => Code
    [2] => Help
    [3] => Pro
)

6. preg_quote() Function

preg_quote() function quotes the match pattern or regular expression. Let’s look at one example to make clear concepts.

<?php

$string = 'Welcome to Code / Help / Pro';
$string = preg_quote($string, '/');
echo $string; 

?>

Regular Expression Modifiers

Regular Expression search depends on the modifiers.

ModifierDescription
iReturn output after the case-insensitive search.
mPerform multiple searches on the string.
uThis will enable the correct matching of UTF-8 encoded regular expression.

Regular Expression Pattern

ExpressionDescription
[abc]Find the one character from the option inside the brackets
[^abc]Detect any character, not inside the brackets
[0-9]Get one number range from 0 to 9