PHP, as befits a modern language.programming, offers the developer a set of functions for using regular expressions. You can search for line entries in other rows by complex criteria.
HTML, CSS, XML and other formalized files -classic tasks for applying the preg match all function. The search for addresses, surnames, phone numbers, e-mail and other information in non-formalized texts has no less effect.
PHP предлагает две функции поиска:preg match and preg match all. The first one looks for the first occurrence of the pattern in the string, the second - all the occurrences. Sometimes the term "pattern match" is used. In the first case, the result of the function is “the string matches the pattern,” in the second case, “the string matches the pattern”. Formally, the term “coincidence” more accurately reflects the essence, but the natural context of an operation is usually “search” for information. In practice, both the one and the other are in demand. The following is the format of the functions.
The result of the function is the number, the number of matches. All matches found are written to the array - matches. In the case of the preg match all function, you can specify the sort order of the array:
Sorting by first option groups the search results by regular expression number (default value). In the second case, the results are grouped by their location in the line.
It is important to remember that the template operates on characters.Programming has long forgotten what a "character" data type is. Modern languages do not fall below the concept of "string", but with respect to the template must be understood: here manipulate characters.
Building a template is, first of all, specifying the desired sequence of characters. If this is clearly understood, then there will be no errors in the template. In any case, it will be much less.
The register in the pattern is important. The first and last characters of the pattern are important. You can specify where the pattern begins and how it ends.
PHP preg match all uses standard regular expression syntax. Square brackets denote one of the characters, which is indicated in them:
Repeat characters are indicated by curly brackets - {n, m} and refer to the previous character.
The syntax provides many options for creating templates, but it’s best to start with the basics, that is, with simple, handwritten, in which complex elements and combinations are missing.
Simply put, by listing the real characters,you need them, specifying the necessary quantities and taking into account that the symbol "^" corresponds to the beginning, and "$" to the end of the line, you can create simple templates. By analyzing real, streamlined regular expressions from qualified professionals, you can gain solid knowledge to create complex preg match all applications. PHP arsenal is not limited to just these two functions, but they are most often used.
Pattern for integer:
Also an integer pattern, but there may be a sign ("+", "-") in front, and there may be extra spaces in front / back:
Similarly:
Using your own templates for preg match all, examples of them on the Internet, analyzing the code of site pages and other sources allow you to create your own template library.
Options for finding information may belots of. In particular, the latter two designs can be modeled differently. In many cases, the preference will be the one that will quickly and accurately provide the desired match. The use of preg match all in PHP, as well as similar functions in other languages, requires practice, attention and preliminary validation of templates.