Regular expressions (PHP). PHP: regular expressions, examples

How large and diverse the world of information is, just as rapidly it is expanding and developing. Any decision is just a moment in the overall process of movement. Knowledge and experience provide an opportunity to understand the process of achieving a goal, but never is the result of achieving it. It is doubtful that in the foreseeable future one can talk about the result in general, but it is very important that the process of striving for it represents an essential and objectively necessary process.

“Desire and / or big money” does not create an algorithm capable of what a person does naturally and “for free”, especially when he does not think about what he is doing. In every position of the information problem there is at least one white spot, but behind it, usually immediately, other clean spots are visible, and it is not at all necessary that they are all white.

Php regular expressions

Internet technologies have greatly simplified access to information, but the question - how to put it on your wish - has not been simplified, but acquired new “volumes” of work. And while in PHP regular expressions obviously do not exhaust the gap, they are a significant step forward.

Simple functions + algorithm = template

Offering regular expressions, PHP significantly reduces the content of the algorithm, but the usual search / replace functions and processing algorithms for the found ones have not lost their meaning. New ideas do not portend the information revolution. The maximum you can count on is the compact code and the growth of the developer’s ability to correctly formulate a search query.

PHP regular expressions examples

You should know and use regular expressions in PHP. The examples are convincing and effective. But you should be aware that with the advent of the new search mechanism, the center of gravity has moved to the template - a kind of mechanism, although in fact it replaces many simple search functions, conditions, loops, and other operators.

Regular expression php number

Have:

  • a high-quality and modern tool is good;
  • to see and use the experience of colleagues is important;
  • Combining reality and virtual ideas is a guarantee of success.

Achieving knowledge of how to understand the problem in order to solve it is often more important than a specific result.

Regular expressions in PHP are represented by several functions in the syntax of the language and many useful examples on the Internet. Due to the specifics of the language syntax, the application is limited only by the imagination of the developer, however, it should be borne in mind that not all templates will work. By means of PHP check of regular expressions is essential.

Simple search for a character (string)

A symbol is not always one byte, and often a symbol, like an elementary signal, carries an exact meaning additionally. The encoding defines the visible characters in the code in different ways. A symbol can have several options: "$" = "USD" = "cu", ... - this is the exact meaning that somewhere something, but not necessarily nearby, is connected with the currency. However, the exact meaning may lie in the PHP variable, which always begins with the symbol "$", but cannot begin with "USD" and "cu".

Regular expressions in php

In programs, a symbol can be a function name (string) in the context of: recognize and execute - this is from the field of dynamic object-oriented programming, when PHP constructs, functions, regular expressions are used directly for decision-making. The found symbol is “executed”, that is, it determines by itself what needs to be done in the place in which it is found, and in the way he “considers it” necessary.

Finding just a character / string is not such an easy task, and the more information in a character, the better. It is not always necessary to find the top ten best food prices, choose delicious holiday cake recipes, or decide on a contractor for a suspended ceiling.

Php regex check

Often it is necessary to optimize simple algorithms with not simple information arithmetic or in difficult conditions, when the desired character must be selected from the database, and the line in which it should be found should be found in hundreds of sites. In this case, the price of choosing a symbol is equal to the time of sampling from the database, and the price of where to look is the time of searching the notorious hundreds of sites.

A lonely symbol is not a phrase or several phrases; it can be anywhere and be arbitrarily small. How to determine what exactly is what is found?

Example: price search

Just finding the $ symbol in the text is not enough. Checking that he is facing a number is also not always enough. Providing a dot or number in front of the “$” symbol is a guarantee that prices with money symbols in front will not be taken into account. Prices will also be skipped, in which the currency is not indicated at all, but on the page there is an explicit indication on it.

Php regular expression functions

In general, the solution of the problem in the usual way will inflate the algorithm beyond recognition and will take a lot of time. Meanwhile, using a regular expression, PHP will find the number without any problems.

$ cContents = preg_match_all ("/ [0-9] + ([\ $] | usd | y \ . \. | ) {1} / i";

"look e-mail - 2usd; <br/>";
"find e-mail - $ 2; <br/>";
"work e-mail - 2; <br/>";
"check e-mail - $ 13", $ aResult);
. '; '. implode (',', $ aResult [0]). '<br/>'.

Will give the answer: "2; 2usd, 2..", But will not find anything in the line: "check e-mail - $ 13".

Providing for the presence of a symbol in the template in front or behind, you can quickly achieve the goal in the vast majority of cases.

$ cContents = preg_match_all ("/ ([0-9] + ([\ $] | usd | y \ . \. | ) {1}) | (([$] | usd | y \ . \. | ) {1} [0-9] +) / i ";
"look e-mail - 2usd; <br/>";
"find e-mail - $ 2; <br/>";
"work e-mail - 2; <br/>";
"check e-mail - $ 13", $ aResult);
. '; '. implode (',', $ aResult [0]). '<br/>'.

Result: "4; 2usd, 2.., 2, $ 13".

About Unintended Logic

PHP offers regular expressions, like other tools, but it is not necessary to use them by its rules or use other lowercase functions in the manner provided by the syntax.

You can turn the search into an array before starting the search, and divide the place where the search will be carried out into components according to a preliminary criterion. A simple pair of explode () and implode () functions often simplify and speed up the solution.

Regular expressions in php

For example, as a result of preliminary work, an array of rows (data) was generated for the region about how many potatoes each village collected: NameDerevnya and the collected volume are indicated, and if there are several enterprises in the village, then the nameDerevnya.NamePredpriyatie and the value through "; " and so on in all enterprises. It will not be possible to calculate the total volume of the collected in this case, but if you make impode (";", $ aResult), there will be one long line in which the pairs - Name of the Village / Name of the Village. Name of the Enterprise - the collected volume - will be listed through ";". Having done the opposite on the obtained one, explode (";", $ aResult) is an array of everyone who collected and how much he collected, then, removing all non-digital characters from the lines, we have only numbers, the sum of which will be the total amount collected.

In this example, regular expressions in PHP allow you to not look for superfluous at all, they simply and easily extract the desired digital values. Quickly and efficiently, no matter how long the names of villages and enterprises are. This solution is not ideal, but it shows that the rules should not always be followed. Often you can achieve the desired unusual way.

Abstracting from the technical side, from the encoding

The algorithm syntax does not say how it really looks. But, providing the same encoding of the page, script and lines (which is searched and in which the search takes place), you can immerse yourself in the task outside its technical side.

Php preg match regex

It is necessary not only to know what to look for, but also where to do it, when, in what quantity, what to change and how. Formally, regular PHP expressions are implemented as a complete proprietary full-featured mechanism, however, for many reasons, the implementation of its analogues in different languages ​​is different. As a general rule, before transferring one or another template to a particular tool environment to solve a specific task, it is necessary to carry out a qualitative adaptation, to check on all possible options for the source data.

Php regular expression space

Even if you use your own PHP development experience, regular expressions, examples, patterns, and constructs require close inspection. The language’s functionality, its syntax and runtime are dynamically changing.

Template structure and content

What was previously written as a combination of search functions, conditional operators, replace, insert, delete operations consists of one line of characters according to certain rules - a regular expression pattern. In fact, a significant amount is encoded in a strictly defined way and executed according to specific rules.

Just as you cannot debug any language operator, you cannot debug a regular expression pattern, this process can only be emulated. As a general rule, the option accepted: works - does not work. The syntax is very simple, although its perception, especially at the beginning of the work, is very difficult.

Simple expression example

In a simple version, the template matches the line:

$ cContents = preg_match ("/ asdf / i", "asdf", $ aResult). '; '. implode (',', $ aResult).

The result will be "1; asdf", since the function finishes its work on the first match of the pattern encountered. The result will be the same if you look in the line "aaaasdf" and in the line "zzzasdfvvv". If you specify the first and / or last character, the result will be unambiguous:

$ cContents = preg_match ("/ ^ asdf / i", "asdf", $ aResult). '; '. implode (',', $ aResult). '<br/>';
$ cContents. = preg_match ("/ asdf $ /", "asdf", $ aResult). '; '. implode (',', $ aResult). '<br/>'.

The result will be: 1; asdf1; asdf.

The characters "^" and "$" indicate where the pattern begins and how it ends. The result of the preg_match function is the number of matches, it can be 0, 1 or false if an error is detected. The third parameter - an array - contains the only element found. In the case of using the preg_match_all () function, the search continues to the end of the line.

$ cContents = preg_match ("/ asdf / i", "asdf123asdf456asdf789", $ aResult). '; '. implode (',', $ aResult). '<br/>';
$ cContents. = preg_match_all ("/ asdf / i", "asdf123asdf456asdf789", $ aResult). '; '. implode (',', $ aResult [0]). '<br/>'.

Result:

1; asdf
3; asdf, asdf, asdf.

If you do not use special instructions, then each character defines itself and its place, regardless of the encoding of the string and the encoding of the template. The programmer must ensure that the encodings of these elements are correct in relation to each other and to the encoding of the page.

PHP functions for working with regular expressions

PHP regular expressions are strings of characters written in a specific way. Usually, by analogy with other programming languages, they are written in the characters "/" ... "/" and placed in quotation marks, depending on the algorithm, you can use single or double.

The main function of php preg match is using regular expressions as a pattern match and stops when it is first found, its variant with the suffix _all searches for all matches and gives an array of those. The preg replace function replaces each found pattern match; it can work with arrays, allowing you to design a search and perform a replacement according to many options.

Interesting options for working with regular expressions can be developed using the preg_replace_callback function, which accepts not the text to replace, but the name of the function that will perform such a replacement. In addition to practical value, it is possible to implement mechanisms for checking templates, which plays a significant role in the development and debugging of the latter.

Regular Expressions

Since hypertext languages ​​are formalized information, they are mainly the source material. A large number of templates and designs have been developed that allow efficient processing of large amounts of information. A significant part of this work is performed by mechanisms built into various site management systems (CMS).

It is not surprising that many CMS have become de facto standards for building sites, and being accompanied (updated) by development companies, they do not make it necessary to carry out the development of templates on their own. However, outside of such systems, having experience with regular expressions is very useful.

Regular expressions and real information

An important area of ​​application of regular expressions in PHP is formed by the advent of PHPOffice libraries. Work with documents and spreadsheets has always been and is important, and the OOXML standard allowed not only to parse real documents, but also to create them programmatically.

Thanks to the development of hypertext languages ​​and PHP in particular, a real opportunity has arisen to automate the "production" of documents of a "standard form", for example, invoices for payments, reports, business plans and other information objects, the structure and contents of which can be formalized and automated.

Due to the use of the OOXML language for describing the regular expression development process, there are significant differences from the traditional work with HTML and CSS markup languages ​​in the first place. A real document, in addition to markup and in addition to the actual content, has many details. For example, a document created automatically by the program will have strict and accurate content. A document created by a person or processed by him will have content, painted according to many tags. Any work with the document leads to the fact that its semantics can remain one, but painted inside by a different number of different designs.

Natural information and situation

A person is always interested in solving a problem, the formulation of which is information and, as a result, information will also be given. By presenting the statement of the problem in the form of a document, a person receives a certain semantics, which, during the refinement by specialists (making changes, clarifying wording, deleting, editing), leads not only to many versions of one document, but also to its versions with different contents.

In particular, the task posed by the phrase “Solve quadratic uranium” after editing will not look like this: “Solve the quadratic equation”. If the first error was corrected first, and then the second, then the natural information in the OOXML style may look like: "[Solve] [] [square] [e] [] [cheers] [c] [n]], but this is not at all guarantee that this is how the task will look in OOXML tags (square brackets emulate tag brackets).

It is easy to gather information together after each task editing process, but each time this procedure will be performed according to a different algorithm. Regular expressions, as a kind of tool for formalizing information tasks, have become, in fact, a path from formal hypertext structures to natural information.

Source: https://habr.com/ru/post/K17673/


All Articles