Php pattern matching

675 views 13 slides Mar 08, 2022
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

Pattern Matching using PHP


Slide Content

Pattern Matching Jigar Makhija

PHP Regular Expression Introduction Regular expressions allow you to search for and replace patterns in strings.

PHP Regular Expression Functions PHP preg_filter() Function Returns a string or an array with pattern matches replaced, but only if matches were found <?php $input = [ "It is 5 o'clock", "40 days", "No numbers here", "In the year 2000" ]; $result = preg_filter('/[0-9]+/', '($0)', $input); print_r($result); ?> O/P: Array ( [0] => It is (5) o'clock [1] => (40) days [3] => In the year (2000) )

PHP Regular Expression Functions PHP preg_grep() Function Returns an array consisting only of elements from the input array which matched the pattern. <?php $input = [ "Red", "Pink", "Green", "Blue", "Purple" ]; $result = preg_grep("/^p/i", $input); print_r($result); ?> O/P: Array ( [1] => Pink [4] => Purple )

PHP Regular Expression Functions PHP preg_last_error() Function Returns an error code indicating the reason that the most recent regular expression call failed. <?php $str = 'The regular expression is invalid.'; $pattern = '/invalid//'; $match = @preg_match($pattern, $str, $matches); if($match === false) { // An error occurred $err = preg_last_error(); if($err == PREG_INTERNAL_ERROR) { echo 'Invalid regular expression.'; } } else if($match) { // A match was found echo $matches[0]; } else { // No matches were found echo 'No matches found'; } ?>

PHP Regular Expression Functions PHP preg_match() Function & PHP preg_match_all() Function Finds the first match of a pattern in a string <?php $str = "Visit W3Schools"; $pattern = "/w3schools/i"; echo preg_match($pattern, $str); ?> Finds all matches of a pattern in a string <?php $str = "The rain in SPAIN falls mainly on the plains."; $pattern = "/ain/i"; if(preg_match_all($pattern, $str, $matches)) { print_r($matches); } ?> Array ( [0] => Array ( [0] => ain [1] => AIN [2] => ain [3] => ain ) )

PHP Regular Expression Functions PHP preg_replace() Function Returns a string where matches of a pattern (or an array of patterns) are replaced with a substring (or an array of substrings) in a given string <?php $str = 'Visit Microsoft!'; $pattern = '/microsoft/i'; echo preg_replace($pattern, 'W3Schools', $str); ?> O/P: Visit W3Schools!

PHP Regular Expression Functions PHP preg_replace_callback() Function Given an expression and a callback, returns a string where all matches of the expression are replaced with the substring returned by the callback <?php function countLetters($matches) { return $matches[0] . '(' . strlen($matches[0]) . ')'; } $input = "Welcome to W3Schools.com!"; $pattern = '/[a-z0-9\.]+/i'; $result = preg_replace_callback($pattern, 'countLetters', $input); echo $result; ?> O/P: Welcome(7) to(2) W3Schools.com(13)!

PHP Regular Expression Functions PHP preg_replace_callback_array() Function Given an array associating expressions with callbacks, returns a string where all matches of each expression are replaced with the substring returned by the callback <?php function countLetters($matches) { return $matches[0] . '[' . strlen($matches[0]) . 'letter]'; } function countDigits($matches) { return $matches[0] . '[' . strlen($matches[0]) . 'digit]'; } $input = "There are 365 days in a year."; $patterns = [ '/\b[a-z]+\b/i' => 'countLetters', '/\b[0-9]+\b/' => 'countDigits' ]; $result = preg_replace_callback_array($patterns, $input); echo $result; ?> O/P: There[5letter] are[3letter] 365[3digit] days[4letter] in[2letter] a[1letter] year[4letter].

PHP Regular Expression Functions PHP preg_split() Function Breaks a string into an array using matches of a regular expression as separators <?php $date = "1970-01-01 00:00:00"; $pattern = "/[-\s:]/"; $components = preg_split($pattern, $date); print_r($components); ?> Array ( [0] => 1970 [1] => 01 [2] => 01 [3] => 00 [4] => 00 [5] => 00 )

PHP Regular Expression Functions PHP preg_quote() Function Escapes characters that have a special meaning in regular expressions by putting a backslash in front of them <?php $search = preg_quote("://", "/"); $input = 'https://www.w3schools.com/'; $pattern = "/$search/"; if(preg_match($pattern, $input)) { echo "The input is a URL."; } else { echo "The input is not a URL."; } ?> O/P: The input is a URL.

Regular Expression Modifiers Modifier Description i Performs a case-insensitive search m Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line) u Enables correct matching of UTF-8 encoded patterns Regular Expression Patterns Expression Description [abc] Find one character from the options between the brackets [^abc] Find any character NOT between the brackets [0-9] Find one character from the range 0 to 9

Metacharacters Metacharacters are characters with a special meaning: Metacharacter Description | Find a match for any one of the patterns separated by | as in: cat|dog|fish . Find just one instance of any character ^ Finds a match as the beginning of a string as in: ^Hello $ Finds a match at the end of the string as in: World$ \d Find a digit \s Find a whitespace character \b Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b \uxxxx Find the Unicode character specified by the hexadecimal number xxxx