PHP: regular expressions, function preg match all

PHP, as it is supposed to modern languageprogramming, offers the developer a set of functions for using regular expressions. You can search for occurrences of strings in other rows by complex criteria.

preg match all

HTML, CSS, XML and other formalized files -classical tasks for applying the function preg match all. No less effective is the search for addresses, surnames, telephone numbers, e-mail and other information in unformalized texts.

Function Format

PHP offers two search functions: preg match and preg match all. The first one looks for the first occurrence of the pattern in the line, the second one - all occurrences. Sometimes the term "match with a pattern" is used. In the first case, the result of the function is "the string is the same as the pattern", in the second case, "the line matches the pattern". Formally, the term "coincidence" more accurately reflects the essence, but the natural context of the operation is usually "searching" for information. In practice, one and the other is in demand. Below is the format of the functions.

php preg match all

The result of the function is the number, the number of matches. All found matches are written to the array - matches. In the case of the function preg match all you can specify the order of the array sorting:

  • PREG_PATTERN_ORDER;
  • PREG_SET_ORDER.

Sorting by the first option groups the search results by the number of the regular expression (the default value). In the second case, the results are grouped according to their location in the line.

A symbol is a template element

It is important to remember that the template operates with symbols. Programming has long forgotten that such a data type is a "symbol". Modern languages ​​do not fall below the notion of "string", but with respect to the template one must understand: here symbols are manipulated.

The construction of a template is, first of all, an indication of the desired sequence of symbols. If this is clearly understood, then there will be no errors in the template. In any case, it will be much less.

  • a is a specific element of the template - a symbol.
  • a-z is a template element, also one character, but only with a value from a to z - the entire Latin alphabet in lowercase.
  • 0-9 is one digit, and any, but 1-3 are only 1, 2 or 3.

The register in the template is important. The first and last characters of the template are of great importance. You can specify how the template starts and how it ends.

Function Template

PHP preg match all uses the standard syntax of regular expressions. The square brackets denote one of the characters that indicate:

  • [abc] only the characters a, b, c.
  • [^ ABC] all except characters A, B, C.
  • \ w and \ W is a text or non-text character.
  • \ s and \ S is a whitespace or non-whitespace character.
  • \ d and \ D is a digit or not a digit.

php preg match all

The repetition symbols are denoted by curly brackets - {n, m} and refer to the previous character.

  • n means repetition "not less than";
  • m - repetition "no more".

The syntax provides many options for creating templates, but it's best to start with the basics, that is, from simple, self-written, in which complex elements and combinations are missing.

Simply put, listing real characters,which are needed by specifying their required quantities and taking into account that the symbol "^" corresponds to the beginning, and "$" is the end of the line, you can create unpretentious templates. Analyzing real debugged regular expressions from qualified specialists, you can gain a solid knowledge for creating complex applications of preg match all. PHP arsenal is not limited to these two functions, but they are most often used.

Simple practice

Template for integer:

  • "/ [0-9] * /"

Also an integer pattern, but there may be a sign ("+", "-") in front, and there may be extra spaces in front / behind:

  • / ^ [\ s | \ + | \ -] {0,1} [0-9] * /

Similarly,

  • /^[\s|\|||]]0,1}[0-9]*(\.)[0-9]*/ - a number with a period.
  • / [0-9a-z_-][email protected][0-9a-z _ ^ \.] + \. [A-z] {2,3} / - option for recognizing e-mail.

Using your own templates for preg match all, their examples on the Internet, the analysis of the code of pages of sites and other sources allow you to create your own library of templates.

preg match all examples

Options for finding information can bea bunch of. In particular, the above two constructions can be modeled differently. In many cases, the preference will be given to the template that will provide the desired match more quickly and accurately. The use of PHP preg match all, as well as similar functions in other languages, requires practice, attention and preliminary verification of the correctness of the templates.

Related news

PHP: regular expressions, function preg match all PHP: regular expressions, function preg match all PHP: regular expressions, function preg match all PHP: regular expressions, function preg match all PHP: regular expressions, function preg match all PHP: regular expressions, function preg match all PHP: regular expressions, function preg match all PHP: regular expressions, function preg match all PHP: regular expressions, function preg match all PHP: regular expressions, function preg match all