How big and diverse the world of information is, how 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 one can speak of a result at all, but it is very important that the process of striving for it is an essential and objectively necessary process.
"Desire and/or big money" does not create an algorithm capable of doing what a person does naturally and "for free", especially when he does not think about what exactly he is doing. In any position of the information task 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.
Internet technologies have greatly simplified access to information, but the question - how to impose on the existing desired - has not been simplified, but acquired new "volumes" of work. And although in PHP regular expressions do not explicitly exhaust the space, they area significant step forward.
Simple functions + algorithm=pattern
By offering regular expressions, PHP significantly reduces the content of the algorithm, but the usual search/replace functions and find processing algorithms have not lost their value. New ideas do not herald 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.
You should know and use regular expressions in PHP. The examples are persuasive and effective. But you should be aware that with the advent of the new search mechanism, the center of gravity has shifted to the template - a kind of mechanism, although in fact it replaces many simple search functions, conditions, loops, and other operators.
- quality and modern tool is good;
- seeing and using the experience of colleagues is important;
- combining reality and virtual ideas is a guarantee of success.
Achieving knowledge of how one had 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 language syntax and many useful examples on the web. Due to the specifics of the syntax of the language, the applications are limited only by the developer's imagination, however, it should be borne in mind that not all templates will work. Using PHP, regular expression checking has an essentialvalue.
Simple search for a character (string)
A character is not always one byte, and often a character, like an elementary signal, carries the exact meaning additionally. The encoding determines the visible characters in the code in different ways. The symbol can have several options: "$"="USD"="cu", … - this is the exact meaning that something somewhere, but not necessarily nearby, is associated with a currency. However, the exact meaning may also lie in a PHP variable that always starts with "$", but cannot start with "USD" and "cu".
In programs, the symbol can be a function name (string) in the context: recognize and execute - this is from the area 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 where it is found, and in the way it "deems" it necessary.
Searching for just a symbol/string is not an easy task, and the more information in a symbol, the better. It's not always necessary to find the top ten food prices, choose delicious birthday cake recipes, or decide on a curtain wall contractor.
Often it is necessary to optimize simple algorithms with not simple information arithmetic or in difficult conditions when the required character needs to be selected from the database, and the line in which it should be found is in a hundredsites. At the same time, 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 for the notorious hundreds of sites.
A lone character is not a phrase or several phrases, it can be anywhere and be arbitrarily small. How to determine what exactly is found is what you are looking for?
Example: find price
Just finding the symbol "$" in the text is not enough. Checking that there is a number in front of it is also not always enough. If you provide a dot or a number before the "$" symbol, this is a guarantee that prices that have money symbols in front will not be taken into account. Prices will also be skipped if the currency is not specified at all, but the page clearly indicates it.
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.
"look e-mail - 2usd;
"find e-mail - 2usd;
"work e-mail - ue2;
"check e-mail - $13", $aResult);
. '; '. implode(', ', $aResult). ''.
Gives the answer: "2; 2usd, 2u", but does not find anything in the line: "check e-mail - $13".
By designing the pattern with a symbol in front or behind, you can quickly reach the goal in the vast majority of cases.
"look e-mail - 2usd;
"find e-mail - 2usd;
"work e-mail - ye2;
"check e-mail - $13", $aResult);
. '; '. implode(', ', $aResult). ''.
Result: "4; 2usd, 2c.u., ye2, $13".
About unintended logic
PHP offers regular expressions like other tools, but it's not necessary to use them according to its rules or to use other string functions in the way provided by the syntax.
Before starting the search, you can turn what you are looking for into an array, 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 simplifies and speeds up the solution.
For example, as a result of preliminary work, an array of rows (data) was formed for the district on how many potatoes each village harvested: VillageName and the harvested volume are indicated, and if there are several enterprises operating in the village, then VillageName. CompanyName goes to the array line and value through ";" and so on for all enterprises. It will not be possible to calculate the total amount collected in this case, but if you do impode(";", $aResult), there will be one long line in which the pairs - VillageName/VillageName. CompanyName - collected volume - will be listed through ";". By doing the opposite on the received, explode(";", $aResult) -an array of all those who collected and how much they collected, then, removing all non-numeric characters from the strings, we have only numbers, the sum of which will be the total amount collected.
In this example, regular expressions in PHP allow you not to search for unnecessary things at all, they simply and easily extract the necessary digital values. Fast and efficient, no matter how long the names of villages and businesses. This solution is not ideal, but it shows that you don't always have to follow the rules. Often you can achieve what you want in an unusual way.
Abstracting from the technical side, from the encoding
The syntax of an algorithm doesn't say what it actually looks like. But by ensuring that the page, script, and strings (which is being searched and which is being searched) have the same encoding, one can dive into the task beyond its technical side.
You need not only to know what to look for, but also where to do it, when, in what quantity, what to change for and how. Formally, PHP regular expressions are implemented as a complete full-featured mechanism of their own, however, for many reasons, the implementation of its analogues in different languages differs. As a general rule, before transferring this or that template to this or that tool environment for solving a specific problem, it is necessary to carry out a qualitative adaptation, to check on all possible variants of the initial data.
Even if you use your own PHP development experience, regular expressions, examples, templates, anddesigns require close scrutiny. The functionality of the language, its syntax and execution environment are dynamically changing.
Structure and content of the template
What was previously written as a combination of search functions, conditional operators, substitution, insertion, deletion operations, is enclosed in one string 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 can't debug any statement in the language, so you can't debug a regular expression pattern, this process can only be emulated. As a general rule, the option is accepted: 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 simple version, the pattern matches the string:
$cContents=preg_match("/asdf/i", "asdf", $aResult). '; '. implode(', ', $aResult).
The result will be "1; asdf" because the function terminates on the first pattern match it encounters. The result will be the same if you search in the string "aaaasdf" and in the string "zzzasdfvvv". If you specify the first and / or last character, then the result will be unambiguous:
$cContents=preg_match("/^asdf/i", "asdf", $aResult). '; '. implode(', ', $aResult). '
$cContents.=preg_match("/asdf$/", "asdf", $aResult). '; '. implode(', ', $aResult). ''.
The result will be:1;asdf1; asdf.
The "^" and "$" symbols indicate where the pattern starts and ends. The result of the preg_match function is the number of matches, it can be 0, 1 or false if an error is found. The third parameter, an array, contains the only found element. In the case of using the preg_match_all() function, the search continues until the end of the string.
$cContents=preg_match("/asdf/i", "asdf123asdf456asdf789", $aResult). '; '. implode(', ', $aResult). '
$cContents.=preg_match_all("/asdf/i", "asdf123asdf456asdf789", $aResult). '; '. implode(', ', $aResult). ''.
1; asdf3; asdf, asdf, asdf.
If you do not use special instructions, then each character defines itself and its place, regardless of the string encoding and template encoding. It is the programmer's responsibility to ensure that the encodings of these elements are correct in relation to each other and to the page encoding.
PHP functions for working with regular expressions
Regular expressions in PHP are strings of characters written in a certain way. Usually, by analogy with other programming languages, they are written in the characters "/" … "/" and placed in quotes, depending on the algorithm, you can use single and double.
The main php preg match function uses regular expressions as a pattern match and stops when it is first found, its variant with the _all suffix looks for all matches and returns an arraythose. The preg replace function replaces every pattern match it finds, and it can operate on arrays, allowing you to design your search and replace across multiple options.
Interesting variants of working with regular expressions can be developed using the preg_replace_callback function, which takes not the text to be replaced, but the name of the function that will perform such a replacement. In addition to practical value, here you can implement template checking mechanisms, which plays a significant role in the development and debugging of the latter.
Scope of regular expressions
Since hypertext languages are formalized information, to a greater extent they are the source material. A large number of templates and designs have been developed to efficiently process large amounts of information. A significant part of this work is done by mechanisms built into various content management systems (CMS).
It's not surprising that many CMS's have become the de facto standard for building websites, and being maintained (updated) by development companies, they don't make it necessary to do template development in-house. However, outside of such systems, having experience with regular expressions is very useful.
Regular expressions and real information
An important use of regular expressions in PHP has been 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 realdocuments, but also generate them programmatically.
Thanks to the development of hypertext languages and PHP in particular, there is a real opportunity to automate the "production" of "standard form" documents, for example, invoices, reports, business plans and other information objects, the structure and content of which can really be formalized and automation.
Due to the use of OOXML to describe the language, the process of developing regular expressions differs significantly 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 generated automatically by a program will have strict and precise content. A document generated by a person or processed by him will have content painted on a variety of tags. Any work with the document leads to the fact that its semantics can remain the same, but painted inside according to a different number of different constructions.
Natural information and situation
A person is always interested in solving a problem, the formulation of which has 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 (introducing changes, clarifying wording, deleting, editing), leads not only to many variants of one document, but also to its versions with different content.
In particular, the problem posed by the phrase "Solve squareequation", after editing will not look like this: "Solve a quadratic equation". If the first error was corrected first, and then the second, then the natural OOXML-style information may look like: "[Solve] [square][e] [cheers][in][no]", but this is not at all a guarantee that this is how the task will look in OOXML tags (square brackets emulate tag brackets).
Collection of information can be easily done 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.