PHP - SPLessons

PHP Regular Expression

Home > Lesson > Chapter 29
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Regular Expression

PHP Regular Expression

shape Description

PHP Regular Expression is a special text string for describing a search pattern. Fundamentally, a regular expression is a pattern describing a certain amount of text. Their name comes from the mathematical theory on which they are founded. The regular expression is abbreviated to regex or regexp. Regex is the most commonly preferred abbreviation, because it is easy to pronounce the plural "regexes". They clearly distinguish the pattern from the surrounding text and punctuation. Arithmetic operators like +, -, ^ are used by Regular Expressions to create complex expressions. Regular expressions helps in validating email addresses, IP address and so on.

Advantages of regular expressions

  • Regular expressions saves coding time by calling only a single function while searching for patterns given by string data.
  • Highlights keywords in search results
  • Regular expressions can be used to identify the template tags and replace them with actual data when creating a custom HTML template.
PHP has two types of regular expression functions which can be used as per the requirements
  • POSIX Regular Expressions
  • PCRE (Perl-Compatible Regular Expressions)

POSIX Regular Expressions

shape Description

POSIX Regular Expression normally looks like an arithmetic expression and small elements of it combine to form a complex expression. PHP presently has 7 functions to search strings with the help of POSIX Regular Expressions.
Function Description
ereg() The function inquires for a string given by a string and returns "true" if the pattern is found, if not false.
ereg_replace() The function inquires for a string defined by a pattern and restores the pattern if found.
eregi() The function examines throughout a string given for a string by a pattern. The search is not case sensitive.
eregi_replace() The function works similar to the ereg_replace(), except search is not case sensitive.
split() The function partitions a string into different elements, where every element boundary depends on the occurrence of string pattern.
spliti() The function works similar to the split(), except that it is not case sensitive.

Perl-Compatible Regular Expressions

shape Description

Perl-Compatible regular expressions are more powerful than the POSIX. Following are the PHP PCRE functions to search strings.

preg_match()

The function preg_match() examines the string for a pattern. If found, returns true and false if not. [php] <?php $my_url = "www.splessons.com"; if (preg_match("/splessons/", $my_url)) { echo "the url $my_url contains splessons"; } else { echo "the url $my_url does not contain splessons"; } ?> [/php] Output: [php]the url www.splessons.com contains splessons[/php] In the above example, => preg_match() is the PHP Regular Expression function => '/splessons/' is the Regular Expression pattern that has to be matched => $my_url is the variable containing the text to be matched against.

preg_split()

The function splits string by a regular expression. [php] <?php // split the phrase by any number of commas or space characters, // which include " ", \r, \t, \n and \f $keywords = preg_split("/[\s,]+/", "SPLessons PHP, Tutorial"); print_r($keywords); ?> [/php] Output: [php] Array ( [0] => SPLessons [1] => PHP [2] => Tutorial )[/php]

preg_replace()

The function preg_replace() works similar to the function ereg_replace(), except that the expressions can be used in the pattern and replacement input parameters. [php] <?php $copy_date = "Copyright 1999"; $copy_date = preg_replace("([0-9]+)", "2000", $copy_date); print $copy_date; ?> [/php] Output: [php] Copyright 2000 [/php]

preg_grep()

The function preg_grep() examines every element of input_array and returns the elements that matches with the regular expression pattern. [php] <?php $foods = array("pasta", "steak", "fish", "potatoes"); // find elements beginning with "p", followed by one or more letters. $p_foods = preg_grep("/p(\w+)/", $foods); print "Found food is " . $p_foods[0]; print "Found food is " . $p_foods[1]; ?> [/php] Output: [php] Found food is pastaFound food is [/php]

preg_ quote()

The function preg_ quote() helps to quote characters of regular expressions. [php] <?php $keywords = '$40 for a g3/400'; $keywords = preg_quote($keywords, '/'); echo $keywords; // returns \$40 for a g3\/400 ?> [/php] Output: [php]\$40 for a g3\/400[/php]

Summary

shape Key Points

  • A regular expression is an algorithm of matching patterns.
  • PHP Regular Expression helps in performing validation checks, creating HTML template systems that recognize tags.
  • PHP has various built-in functions like preg_match, preg_split and preg_replace that support regular expressions.