How large and diverse is the world of informationas rapidly it expands and develops. Any decision is just a moment in the general process of movement. Knowledge and experience make it possible to understand the process of achieving the goal, but never the result of achieving it. It is doubtful that in the foreseeable future we can generally speak of a result, but it is very important that the process of striving for it is an essential and objectively necessary process.
“Desire and / or big money” do not createan algorithm capable of that which is natural and “free” a person does, especially when he does not think about what exactly he does. In any position of the information task there is at least one white spot, but behind it, usually immediately, you can see other clean spots, and it is not at all necessary that they are all white flowers.
Internet technology has significantly simplified accessto the information, but the question - how to impose on what is desired - was not simplified, but acquired new “volumes” of work. And although regular expressions in PHP clearly do not exhaust the space, they are a significant step forward.
Simple functions + algorithm = pattern
Offering regular expressions, PHP is essentiallyreduces the content of the algorithm, but the usual search / replace functions and processing algorithms found have not lost their meaning. New ideas do not portend an information revolution. The maximum you can count on is a compact code and an increase in the developer’s ability to correctly formulate a search query.
Should know and use regular PHPexpressions. Examples are convincing and effective. But you should be aware that with the advent of the search mechanism, the center of gravity has moved into the template - a kind of mechanism, although in essence replacing the set of simple search functions, conditions, cycles, other operators.
Have:
- 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 a task in order to solve it is more often more important than a specific result.
PHP regular expressions are representedseveral functions in the syntax of the language and many useful examples on the Internet. Due to the nature of the syntax of the language, application options are limited only by the developer’s imagination, but it should be borne in mind that not all templates will work. With PHP, regular expression checking is essential.
Simple symbol search (strings)
Символ - это не всегда один байт, и часто символ, as an elementary signal, carries an exact meaning further. 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, but not necessarily close, is connected with the currency. However, the exact meaning may also lie in the PHP variable, which always begins with the "$" symbol, but cannot begin with "USD" and "cu".
In programs, the symbol may be the name of a function.(string) in context: to recognize and execute - this is from the field of dynamic object-oriented programming, when constructs, functions, regular PHP expressions are used directly to make decisions. The found symbol "is executed", that is, it determines by itself what to do in the place in which it is found, and in the way that it "itself considers it" necessary.
Searching just a character / string is not so simple.occupation, and the more information in the symbol, the better. It is not always necessary to find a dozen of the best food prices, choose delicious festive cake recipes or decide on a contractor for a suspended ceiling.
Often it is necessary to optimize simple algorithms.with not simple information arithmetic or in difficult conditions, when the desired symbol must be selected from the database, and the line in which it should be found - in hundreds of sites. At the same time, the price of choosing a symbol is equal to the sample time from the database, and the price of where to look is the search time of the notorious hundreds of sites.
A lonely symbol is not a phrase and not a few phrases, it can be anywhere and be arbitrarily small. How to determine what exactly is the desired?
Example: price search
Simply finding the "$" symbol in the text is not enough.Check that the number in front of him is not always enough. Providing a point or a digit 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 monetary unit is not indicated at all, but there is an explicit indication on the page.
In general, solving the problem in the usual way will inflate the algorithm beyond recognition and take a lot of time. Meanwhile, using a regular expression, PHP will find the number without problems.
$ cContents = preg_match_all ("/ [0-9] + ([$] | usd | cu | ye) {1} / i";
"look e-mail - 2usd;
";
"find e-mail - 2u.e;
";
"work e-mail - ye2;
";
"check email - $ 13", $ a Result);
. ";". implode (",", $ aResult [0]). "
".
It will give the answer: "2; 2usd, 2u.e.", But will not find anything in the line: "check e-mail - $ 13".
By providing a symbol in the template at the front or back, you can quickly achieve the goal in most cases.
$ cContents = preg_match_all ("/ ([0-9] + ([$] | usd | cu | ye) {1}) | (([$] | usd | cu | ye) {1 } [0-9] +) / i ";
"look e-mail - 2usd;
";
"find e-mail - 2u.e;
";
"work e-mail - ye2;
";
"check email - $ 13", $ a Result);
. ";". implode (",", $ aResult [0]). "
".
Result: "4; 2usd, 2u.e, ye2, $ 13".
About unforeseen logic
Регулярные выражения PHP предлагает, как и другие tools, but it is not necessary to use them according to its rules or use other string functions in the manner provided by the syntax.
You can turn the search intoarray, and the place where the search will be carried out, divided into components by a preliminary criterion. A simple pair of explode () and implode () functions often simplify and speed up the solution.
For example, as a result of preliminary workAn array of strings (data) was formed in the district about how many potatoes each village collected: the Name of the Village and the collected volume is indicated, and if there are several enterprises in the village, then the Name of the village is followed by the Name of the Village. The Name and Value through ";" and so on all enterprises. Calculate the total amount collected in this case will not be possible, but if you do impode (";", $ aResult), there will be one long line in which the pairs - VillageName / VillageName. NameThe company - the assembled volume - will be listed through ";". Doing the opposite on the received, explode (";", $ aResult) - an array of everyone who collected and how much he collected, then removing all non-digit characters from the lines, we have only numbers, the sum of which will be the total amount collected.
In this example, regular expressions in PHPthey allow you not to look for the superfluous at all, they simply and easily extract the necessary digital values. Quickly and efficiently, no matter how long the names of villages and enterprises are. This decision is not ideal, but it shows that it is not always necessary to follow the rules. Often you can achieve the desired in an unusual way.
Abstracting from the technical side, from encoding
Синтаксис алгоритма не говорит о том, как он looks really. But, having provided the same coding of the page, the script and the strings (which is searched for and in which the search takes place), you can immerse yourself in the task beyond its technical side.
Нужно не только знать, что искать, но и где это do when, in what quantity, what to change and how. Formally, PHP regular expressions are implemented as a complete, fully-functional mechanism of their own, but for many reasons the implementation of its analogues in different languages is different. As a general rule, before transferring one or another template to one or another instrumental environment to solve a specific task, it is necessary to carry out a high-quality adaptation, perform a check on all possible variants of the initial data.
Даже если используется собственный опыт PHP development, regular expressions, examples, patterns and constructions require close checking. The language functional, syntax, and execution environment change dynamically.
The structure and content of the template
What was previously written as a combinationfunctions of search, conditional operators, operations of replacement, insertion, deletion, is in one line of characters according to certain rules - a regular expression pattern. In fact, a significant amount is encoded in a strictly defined manner and executed according to specific rules.
As well as you can not debug anyoperator language, so you can not debug a regular expression pattern, this process can only be emulated. As a general rule, the option is adopted: it works - it does not work. The syntax is very simple, although its perception, especially at the beginning of work, is very difficult.
Simple expression example
In the simplest version, the pattern matches the string:
$ contents = preg_match ("/ asdf / i", "asdf", $ Result). ";". implode (",", $ Result).
The result will be "1; asdf" because the functionfinishes 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:
$ contents = preg_match ("/ ^ asdf / i", "asdf", $ Result). ";". implode (",", $ aResult). "
";
$ cContents. = preg_match ("/ asdf $ /", "asdf", $ aResult). ";". implode (",", $ aResult). "
".
The result will be: 1; asdf1; asdf.
The characters "^" and "$" indicate where it startstemplate 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). "
";
$ cContents. = preg_match_all ("/ asdf / i", "asdf123asdf456asdf789", $ aResult). ";". implode (",", $ aResult [0]). "
".
Result:
one; asdf
3; asdf, asdf, asdf.
If you do not use special instructions, theneach character defines itself and its place, regardless of the encoding of the string and the encoding of the template. The programmer must make sure 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 arecharacter strings 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 regularuses expression 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 regularexpressions 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, here you can implement mechanisms for checking templates, which in the development and debugging of the latter plays a significant role.
Regular Expressions
Because hypertext languages areformalized information, to a greater extent, they are 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).
No wonder many CMS have become standards.de facto 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 onPHP is shaped by the advent of the PHPOffice libraries. Working 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 and PHP languages inin particular, there was a real opportunity 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 really be formalized and automated.
In connection with the application for the description of the OOXML languageThe regular expression development process has significant differences from 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 statement 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 in the course of clarification 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 "Solvesquare uranium ", after editing it 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] [] [hooray] [in] [option] ", but this is not a guarantee that this is how the task will look in OOXML tags (square brackets emulate tag brackets).
Gathering information together is easy aftereach 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.