Reg function in php

PHP Regular Expressions

Summary: in this tutorial, you’ll learn about PHP regular expressions and functions that work with regular expression including preg_match() , preg_match_all() , and preg_replace() .

Introduction to the PHP regular expressions

PHP string functions allow you to test if a string contains a substring ( str_contains() ) or to replace all occurrences of a substring with another string ( str_replace() ).

However, these functions deal with fixed patterns. They won’t work with flexible patterns. For example, if you want to search any numbers in a string, the str_contains() won’t work.

To search or replace a string using a pattern, you use regular expressions.

A regular expression is a string that describes a pattern such as phone numbers, credit card numbers, and email addresses.

Create regular expressions

To create a regular expression, you place a pattern in forward-slashes like this:

'/pattern/';Code language: PHP (php)
 $pattern = '/\d+/';Code language: PHP (php)

The $pattern is a string. Also, it is a regular expression that matches a number with one or more digits. For example, it matches the numbers 1, 20, 300, etc.

Note that you’ll learn how to form flexible regular expressions in the following tutorial.

The forward-slashes are delimiters. The delimiters can be one of the following characters ~ , ! , @ , # , $ or braces including <> , () , [] , <> . The braces help improve regular expressions’ readability in some cases.

Note that you cannot use the alphanumeric, multi-byte, and backslashes ( \ ) as delimiters.

The following regular expression uses the curly braces as delimiters:

 $pattern = '';Code language: PHP (php)

Search strings using regular expressions

To search a string for a match to a pattern, you use the preg_match() and preg_match_all() functions.

PHP preg_match() function

To search based on a regular expression, you use the preg_match() function. For example:

 $pattern = ''; $message = 'PHP 8 was released on November 26, 2020'; if (preg_match($pattern, $message)) < echo "match"; > else < echo "not match"; >Code language: PHP (php)
matchCode language: PHP (php)

The preg_match() searches the $message for a match to the $pattern .

The preg_match() function returns 1 if there is a match in the $message , 0 if it doesn’t, or false on failure.

To get the text that matches the pattern, you add the third parameter to the preg_match() function like the following example:

 $pattern = ''; $message = 'PHP 8 was released on November 26, 2020'; if (preg_match($pattern, $message, $matches)) Code language: PHP (php)
Array ( [0] => 8 )Code language: PHP (php)

The $matches parameter contains all the matches. The $matches[0] stores the text that matches the pattern. In this example, it is the number 8.

The $matches[1] , $matches[2] , … store the texts that match the first, second,… capturing group —more on this in the capturing group tutorial.

The preg_match() only returns the first match and stops searching as soon as it finds the first one. To find all matches, you use the preg_match_all() function.

PHP preg_match_all() function

The preg_match_all() function searches for all matches to a regular expression. For example:

 $pattern = ''; $message = 'PHP 8 was released on November 26, 2020'; if (preg_match_all($pattern, $message, $matches)) Code language: PHP (php)
Array ( [0] => Array ( [0] => 8 [1] => 26 [2] => 2020 ) )Code language: PHP (php)

In this example, the preg_match_all() puts all matches in a multidimensional array with the first element contains the texts ( 8 , 26 , and 2020 ) that match the pattern.

The preg_match_all() function returns the number of matches, which can be zero or a positive number.

Replace strings using regular expressions

To replace strings that match a regular expression, you use the preg_replace() function. For example:

 $pattern = '/\d+/'; $message = 'PHP 8 was released on 11/26/2020'; echo preg_replace($pattern, '%d', $message); Code language: PHP (php)
PHP %d was released on %d/%d/%d

In this example, the preg_replace() function replaces all numbers in the $message with the string %d .

Summary

  • PHP regular expressions are strings with pattern enclosing in delimiters for example «/pattern/» .
  • The preg_match() function searches for a match to a pattern in a string.
  • The preg_match_all() function searches for all matches to a pattern in a string.
  • The preg_replace() function searches a string for matches to a pattern and replaces them with a new string or pattern.

Источник

Regex in PHP

The regular expression is basically started from mathematics which defines a range of characters for some purpose. Different languages get references from this mathematical concept and applied it for searching patterns and validation of data. In this article, you will learn regex in PHP.

There are many use cases of regex in PHP like searching, validating, etc. In this article, you will learn about regular expressions in search patterns. Once you will be able to understand the regex, you can use it anywhere in PHP.

A regular expression can be used to search text from strings or to replace specific characters or strings.

A regular expression can be a single character or combination of some complex patterns.

Syntax of regular expression in PHP

The delimiter can be any character that is not a letter, number, backslash, or space. The most common delimiter is the forward-slash (/), but when your pattern contains forward slashes it is convenient to choose other delimiters such as # or ~.

Generally, regular expression in PHP consists of three parts.

  1. Delimiter – It specifies that tells the beginning or end of the string in the pattern. A delimiter cannot be a backslash, number, letter, or space. However, a forward slash can be used as a delimiter. If the patterns you are searching for consist of forwarding slashes, you can change the delimiter to tilt ~ or hash #.
  2. Pattern – It specified the pattern or sequence of characters to search.
  3. Modifiers (optional) – You can make the matching of pattern cases insensitive or sensitive.

RegextFunctions in PHP

PHP provides built-in functions for regular expressions. The most common regex functions in PHP are:

  • Preg_replace() – It counts the number of times the pattern occurs in the string and return the count.
  • Preg_match() – returns 0 in case of pattern not found in the string. 1 in case of found.
  • Preg_match_all() – This function replaces the string with another string where the pattern is matched. You will get a more clear idea in the next section if you are finding it hard to understand.

preg_match function in PHP

Preg_match function returns the binary result. That is either 0 or 1. If the string contains the pattern you specify in the first argument of the preg_match function, it will return 1. 0 in case of not found.

Look at the following example, in which we search the string for characters PHP.

preg_match_all function in PHP

This function does not tell if the pattern exists in the string or not. It returns the count of the number of times a pattern appears in the string. Look at the following example in which we have performed a case-insensitive search of patterns ing in the given string.

/i in the above example specify that the string can be either in lower case or upper case.

preg_replace function in PHP

preg_replace function returns a new string by replacing the pattern found in the string with some other string of characters. This function accepts three parameters. The first parameter is the pattern to search in the string in the second parameter. Finally, the third parameter is the string to put in place of the pattern found in the string.

Look at the following example, in which we replace the word website with PHP.org

Regular Expression Modifiers

You can adjust the search behavior of the regex in PHP by defining the following modifier variables.

  1. I – Used to perform case-insensitive searching.
  2. U – For the correct searching of utf-8 patterns.
  3. M – Adds multi-searching in the PHP regex. There are some patterns that search at the beginning or end of each line. So, these patterns look at the beginning and end of each line of the string.

Specify range in PHP regex

Understand the following three concepts. It will help you out to put the range in patterns while searching text or replacing it.

  1. [abc] – The square brackets indicated that find any one character from these characters in the string.
  2. [^0-9] – Look for the characters not in the range within the brackets.
  3. 3 – Find any one character in between 0 to 9. 0 and 9 are inclusive in this case.

You can learn more about Regex functions in PHP here

Источник

Читайте также:  What is operator in java with example
Оцените статью