ComputersProgramming

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

As far as the world of information is large and diverse, it is expanding so rapidly and developing. Any solution is just a moment in the general process of motion. Knowledge and experience make it possible to understand the process of achieving the goal, but never - not the result of achieving it. It is doubtful that in the foreseeable future it is generally possible to talk about the 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" does not create an algorithm capable of what a person naturally and "free" does, especially when he does not think about what he does. In any position of the information problem, there is at least one white spot, but behind it, usually immediately, other blank spots are visible, and it is not necessarily that they are all white.

Internet technologies significantly simplified access to information, but the question - how to impose on the desired one - was not simplified, but acquired new "volumes" of work. And although in PHP regular expressions the gap is clearly not exhausted, 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 found have not lost their value. New ideas do not foreshow the information revolution. The maximum you can count on is a compact code and the developer's ability to correctly formulate a search query.

You should know and apply regular expressions to PHP. The examples are convincing and effective. But it should be reported that with the advent of the new search engine, the center of gravity has shifted to a template - a kind of mechanism, although in effect it replaces many simple search functions, conditions, cycles, and other operators.

Have:

  • A quality and modern tool is good;
  • To see and use the experience of colleagues is important;
  • To combine real reality and virtual ideas - a guarantee of success.

The achievement of knowledge, as it was necessary to understand the problem in order to solve it, often has more significance than a concrete 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 syntax of the language, the application options are limited only by the imagination of the developer, however, it should be noted that not all templates will work. With PHP, checking regular expressions is essential.

Simple search for a character (string)

A symbol is not always one byte, and often a character, as an elementary signal, carries an exact meaning in addition. The encoding defines the visible characters in the code in different ways. The symbol can have several options: "$" = "USD" = "cu", ... is the exact meaning that somewhere somewhere, but not necessarily nearby, is related to the currency. However, the exact meaning can also lie in the PHP variable, which always starts with the "$" symbol, but can not begin with "USD" and "cu".

In programs, the symbol can be the name of a function (string) in the context: recognize and execute is from the area of dynamic object-oriented programming, where the constructs, functions, regular expressions PHP are used directly for decision making. The found symbol is "executed", that is, it determines by itself what to do in the place where it is found, and how it "deems it" to be necessary.

To search simply for a character / string is not such a simple occupation, and the more information is in a symbol, the better. Do not always need to find a dozen of the best food prices, choose delicious recipes for a holiday cake 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 should be selected from the database, and the line in which it should be found - in a hundred 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 for is the search time of the notorious hundreds of sites.

A lone symbol is not a phrase or a few phrases, it can be anywhere and be as small as you like. How to determine what exactly is found is what you are looking for?

Example: price search

Just find the symbol "$" in the text - a little. Check that there is a figure before it, too, is not always enough. If you provide a point or figure before the symbol "$", it is a guarantee that prices, in which the money symbols are in front, will not be taken into account. Prices will also be missed, in which the currency is not indicated at all, but there is an explicit indication on the page.

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

$ CContents = preg_match_all ("/ [0-9] + ([\ $] | usd | у \. E \. | Уе) {1} / i";

"Look e-mail - 2usd;
";
"Find e-mail - 2e,
";
"Work e-mail - уе2;
";
"Check e-mail - $ 13", $ aResult);
. '; '. Implode (',', $ aResult [0]). '
'.

The answer is: "2; 2usd, 2u.e.", but will not find anything in the line: "check e-mail - $ 13".

Provided in the template the presence of a symbol in front or behind, you can quickly reach the goal in the vast majority of cases.

$ CContents = preg_match_all ("/ ([0-9] + ([\ $] | usd | у \. E \. | Уе) {1}) | (([$] | usd | у \ .е \. | Ye) {1} [0-9] +) / i ";
"Look e-mail - 2usd;
";
"Find e-mail - 2e,
";
"Work e-mail - уе2;
";
"Check e-mail - $ 13", $ aResult);
. '; '. Implode (',', $ aResult [0]). '
'.

The result: "4; 2usd, 2u.e., ye2, $ 13".

About unintended logic

Regular expressions PHP offers, like other tools, but it is not necessary to use them according to its rules or use other line functions provided by the syntax.

You can turn the search string into an array before starting the search, and break down into components by 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) has been formed around the district about how many villages each village has collected: the Name of the Village and the amount collected, and if there are several enterprises in the village, then the Name of the Forest goes to the array string. The name of the Enterprise and the value through "; " And so on all enterprises. It is not possible to calculate the total amount collected in this case, but if you make impode (";", $ aResult), there will be one long line in which the pairs - Name Tree / Name Tree. The name of the Enterprise - the collected volume - will be listed through ";". Having done the reverse on the received, explode (";", $ aResult) - an array of all who collected and how many it collected, then, removing all non-numeric characters from the lines, we have only the digits, the sum of which is the total volume of the collected.

In this example, regular expressions in PHP allow you not to search for unnecessary, they simply and easily extract the desired numerical values. Quickly and effectively, no matter how long the names of the villages and businesses are. This decision is not ideal, but it shows that it is not always necessary to follow the rules. It is often possible to achieve the desired in an unusual way.

Abstracting from the technical side, from the encoding

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

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 executed as a complete, complete, fully functional mechanism, but for many reasons the implementation of its counterparts in different languages is different. As a general rule, before you can transfer a template to a particular tool environment for a specific task, you need to perform a qualitative adaptation, perform a check on all possible variants of the original data.

Even if you use your own development experience in PHP, regular expressions, examples, templates and constructs require close verification. The language functional, its syntax and the execution environment dynamically change.

Structure and content of the template

What was previously written in the form of a combination of search functions, conditional operators, replace operations, insertion, deletion, is a single string of characters according to certain rules - a regular expression pattern. In fact, a significant volume is coded in a strictly defined way and is executed according to specific rules.

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

Example of a simple expression

In a simple version, the pattern matches the line:

$ CContents = preg_match ("/ asdf / i", "asdf", $ aResult). '; '. Implode (',', $ aResult).

The result is "1; asdf", since the function finishes its work on the first match of the pattern. 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). '
';
$ CContents. = Preg_match ("/ asdf $ /", "asdf", $ aResult). '; '. Implode (',', $ aResult). '
'.

The result will be: 1; Asdf1; Asdf.

The characters "^" and "$" indicate where the template 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 is an array - the only element found is located. If you use 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:

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 is obliged to take care that the encodings of these elements are correct relative to each other and the encoding of the page.

PHP functions for working with regular expressions

Regular expressions in PHP are character strings 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 function of php preg match regular expressions uses as a pattern match and stops when it first finds it, its variant with the _all suffix looks for all matches and returns an array of those. The preg replace function replaces each matching pattern match, it can work with arrays, allowing you to design a search and perform a replacement for a variety of options.

Interesting options for working with regular expressions can be developed using the function preg_replace_callback, which takes no text to replace, but the name of the function that will perform such a replacement. In addition to the practical value, you can implement template validation mechanisms, which plays an important role in developing and debugging the latter.

The scope of regular expressions

Since hypertext languages are formalized information, they are more of a source material. A large number of templates and designs have been developed to effectively handle large amounts of information. A significant part of this work is done by mechanisms built into various content management systems (CMS).

Not surprisingly, many CMS have become de facto standards for building websites, and, being accompanied by (updated) development companies, they do not need to do 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 appearance of PHPOffice libraries. Work with documents and spreadsheets has always been and is important, and the OOXML standard allowed not only to disassemble the real documents, but also to form them programmatically.

Thanks to the development of Hypertext and PHP in particular, there is a real opportunity to automate the "production" of "standard form" documents, for example, bills for payment, reports, business plans and other information objects whose structure and content can be formalized and automated.

In connection with the application for describing the OOXML language, the process of developing regular expressions has significant differences from the traditional work with markup languages HTML and CSS in the first place. A real document, besides markup and in addition to the actual content itself, has many details. For example, a document created automatically by the program will have strict and precise content. A document formed by a person or processed by it will have content written on a set of tags. Any work with the document leads to the fact that the semantics of it can remain one, but painted inside by a different number of different designs.

Natural information and situation

A person is always interested in the solution of a problem, the statement of which is information and, as a result, information will also be given. Having presented the statement of the problem in the form of a document, a person receives a certain semantics, which during the specification by specialists (making changes, clarifying the wording, deleting, editing) leads not only to the many variants of one document, but also to versions with different content.

In particular, the task posed by the phrase "Solve quadratic escape", after editing will not look like this: "Solve the quadratic equation". If the first error was corrected first and then the second one, then the natural information in the OOXML style can look like: "[Solve] [] [squarely] [e] [] [hurray] [in] [nenia]", but this is not at all Guarantee that this is how the task will look in the OOXML tags (square brackets emulate the parentheses of the tags).

You can gather information together easily after each process of task editing, 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.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.birmiss.com. Theme powered by WordPress.