PHP REGEX: HOW TO USE DIFFERENT REGEX FUNCTIONS
What Will I Learn?
- You will learn how to use the different regex functions in php
Requirements
- WAMP/MAMP/LAMP/XAMP or any other PHP/MYSQL STACK
Difficulty
- Intermediate
Tutorial Contents
preg_match()
Used for searching strings and returning true if a match is found and false if no match was found.
preg_match ( $pattern , $subject , $matches, $flags, $offset)
- $pattern refers to the regex pattern rule
- $subject refers to the string to be matched
- $matches will contain the result of matches
- $flags refers to certain changes in the way the matches are returned.An example of a flag is
PREG_OFFSET_CAPTUREwhich returns the string offset together with the string in the match array returned. - $offset is used to offset the position where the search is to begin in the string.
Example:
$subject = "opensource";
$pattern = '/^source/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 6);
print_r($matches);
The above script will return an empty array becuase it has been offseted in 6 places thereby leaving the supposed array element source which could have been returned if there was no offset of 6.
Example:
$subject = "opensource";
$pattern = '/^source/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
In the above example where the offset has been removed, the script will return source because no offset was set
preg_match_all()
returns all the matches found in a string
preg_match_all ($pattern , $subject , $matches , $flags, $offset)
- $pattern refers to the regex pattern rule
- $subject refers to the string to be matched
- $matches will contain a multi-dimensional array contianing the result of matches
- $flags refers to certain changes in the way the matches are returned.
There are three types of flasgs that can be used here:
PREG_PATTERN_ORDER,PREG_OFFSET_CAPTURE&PREG_SET_ORDER
PREG_PATTERN_ORDER gives two types of sub-arrays in the multi-dimensional arrays. The first array (matches[0]) gives the matches together with the other pattern element in the rule while the second array gives the matches alone without the pattern.
This is further explained in the examples below.
PREG_SET_ORDER simply returns the matches depending on the number of stated matches in the array. It returns it together with the oatterns.
This is further explained in the examples below.
PREG_OFFSET_CAPTURE gives an array of arrays that contain the matches at key 0 and their offset at key 1.
This is further explained in the examples below.
- $offset is used to offset the position where the search is to begin in the string.
Example: (PREG_PATTERN_ORDER)
$subject = "|<[^>]+>(.*)</[^>]+>|U";
$pattern = "<b>Tutorial: </b><div align=left>Regex Rules</div>";
preg_match_all($subject,$pattern,$results, PREG_PATTERN_ORDER);
echo $results[0][0] . ", " . $results[0][1] . "\n";
echo $results[1][0] . ", " . $results[1][1] . "\n";
The above will give this output below:
<b>Tutorial: </b><div align=left>Regex Rules</div>
Tutorial: , Regex Rules
Example: (PREG_OFFSET_CAPTURE)
preg_match_all('/(steem)(it)/', 'steemit', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
The above will give this output below:
Array
(
[0] => Array
(
[0] => Array
(
[0] => steemit
[1] => 0
)
)
[1] => Array
(
[0] => Array
(
[0] => steem
[1] => 0
)
)
[2] => Array
(
[0] => Array
(
[0] => it
[1] => 5
)
)
)
Example: (PREG_SET_ORDER)
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
"<b>Tutorial: </b><div>Regex Rules</div>",
$out, PREG_SET_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
The above will give this output below:
<b>Tutorial: </b>
<div>Regex Rules</div>
preg_replace()
used for replacing a set of characters in a string
preg_replace ($pattern ,$replacement , $subject , $limit ,$count )
- $pattern refers to the regex pattern rule
- $replacement refers to the string or arryay to replace
- $subject refers to the string to be saerched for replacement
Take Note: $pattern and $replacement could be arrays too
- $limit stated the limits fod the number of replacement to be made
- $count, if stated , will contain the number of replacements made.
Example:
$string = 'The blue benz car fell off the bridge';
$patterns = array();
$patterns[0] = '/blue/';
$patterns[1] = '/benz/';
$patterns[2] = '/car/';
$replacements = array();
$replacements[2] = 'black';
$replacements[1] = 'suzuki';
$replacements[0] = 'bike';
echo preg_replace($patterns, $replacements, $string);
The above will give this output below:
The black suzuki ride fell off the bridge
preg_split()
used for splitting a set of characters in a string
preg_split ($pattern , $subject , $limit, $flags)
- $pattern refers to the regex pattern rule
- $subject refers to the string we are searching
- $limit specifies the number of sub-strings to be returned.
- $flags refers to any of the following:
*- PREG_SPLIT_NO_EMPTY
This only allows non-empty space being returned - PREG_SPLIT_DELIM_CAPTURE
THis allows the return of parenthesized results - PREG_SPLIT_OFFSET_CAPTURE
This allows bundles the appendant string offset with the strings when they are being returned.
Example:
$string = 'color';
$ret = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($ret);
The above will give this output below:
Array
(
[0] => c
[1] => o
[2] => l
[3] => o
[4] => r
)
preg_grep()
Searches an array to find matching elements
preg_grep ( $pattern , $input ,$flags)
- $pattern refers to the regex pattern rule
- $input refers to the input array
- $flags : The only known flag here is
PREG_GREP_INVERT. When set, this returns the elements of the input array that do match the set pattern
Example:
$input = array('ball', 'keg', 'head', 'blue', grape', 'books', 'can');
$ret = preg_grep("/ball|blue|look/", $input);
print_r($ret);
The above will give this output below:
Array
(
[0] => ball
[3] => blue
)
preg_ quote()
preg_quote ($str, $delim)
- $pattern refers to the input string
- $input is used for escaping the delimiter required by the PCRE functions
Example:
$str = '$40 for a g3/400';
$ret = preg_quote($keywords, '/');
echo $ret;
The above will give this output below:
\$40 for a g3\/400
Curriculum
PHP FORM SERIES
- PHP FORM SERIES: How to receive form data in PHP (POST AND GET METHOD)
- PHP FORM SERIES: How to format form data
- PHP FORM SERIES: How to insert data into database
PHP REGEX
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @shreyasgune, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Hey @akintunde I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x