PHP and regular expressions. A regular expression from a swords blog is a specific format that can be used to identify the usage of a string in another string. Several programming languages, including blogs from swords
Let's take a look at what regular expressions are and why you should use them in PHP programs.
What is a regular expression?
What do you think of separating a program like BBEdit and notepad from a good old control-based text editor? Both support text input, allowing you to save text to a file, but the current text editor also supports other features, including the search-replace tool, which makes it quite easy to edit a text file.
Regular expressions are similar, but better. Regular expressions can be considered as an extremely advanced search-replacement tool, which frees us from the pain: you do not have to write custom data validation examples to check the email address or confirm that the phone number format is correct.
One of the most common functions in any program is the data validity check. PHP binds some text check functions, allowing us to use regular expressions to match a string, to confirm that there is a space, there is a question mark, and so on.
What you don't know is, can regular expressions be simply equipped, when you have mastered some regular expressions (this regular expression can be used to tell the regular expression engine what we want to match in a string ), you may ask why the regular expression has been thrown into the corner for so long, ^ _ ^.
PHP has two sets of functions for processing two types of regular expressions: Perl5 compatibility mode and Posix standard compatibility mode. In this article, we will take a look at the ereg function and use a search expression following the Posix standard. Although they are not as powerful as Perl5, they are a good way to learn regular expressions. If you are interested in the Perl5 compatible regular expressions supported by PHP, go to the PHP.net website to find some details about the preg function.
PHP has six functions to process regular expressions. they all take a regular expression as their first parameter and list it as follows:
Why use a regular expression?
|• Ereg: The most common regular expression function. ereg allows us to search for a string matching a regular expression.
• Ereg_replace: allows us to search for a string matching a regular expression and use a new string to replace all the places where this expression appears.
• Eregi: The effect is almost the same as that of ereg, but case-insensitive.
• Eregi_replace: The same search-replace function as ereg_replace, but case-insensitive.
• Split: allows us to search for strings matching regular expressions and return matching results in the form of string sets.
• Spliti: the split function ignores case-insensitive versions.
If you constantly create different functions to check or manipulate part of a string, you may have to discard all these functions and replace them with regular expressions. If you answer "yes" to any of the following questions, you must consider using a regular expression:
• Are you writing custom functions to check form data (for example, one @ or one point in the email address )?
• Do you want to write custom functions to cycle every character in a string? if this character matches a specific feature (for example, it is capitalized or it is a space ), then replace it?
In addition to the uncomfortable string check and operation methods, if you do not write code efficiently, the above two will also slow your program down. Do you prefer to use the following code to check an email address:
| Function validateEmail ($ email)
$ HasAtSymbol = strpos ($ email ,"@");
$ HasDot = strpos ($ email ,".");
If ($ hasAtSymbol & $ hasDot)
Echo validateEmail ("email@example.com ");
... Or use the following code:
Function validateEmail ($ email)
Return ereg ("^ [a-zA-Z] + @ [a-zA-Z] +. [a-zA-Z] + $", $ email );
Echo validateEmail ("firstname.lastname@example.org ");
Certainly, the first function is relatively easy and looks good in structure. But isn't it easier to use the email address check function of the next version?
The second function shown above only uses regular expressions, including a call to the ereg function. The Ereg function returns true or false to declare whether its string parameter matches the regular expression.
Many programmers avoid regular expressions because they (in some cases) are slower than other text processing methods. Regular expressions may be slow because they involve copying and pasting strings in memory, because each new part of the regular expression matches a string. However, in my experience with regular expressions, unless you run a complex regular expression on hundreds of lines in the text, the performance defects are negligible, this is also rare when you use a regular expression as the input data check tool.
Regular expression syntax
Before you can match a string to a regular expression, you must first create a regular expression. At the beginning, the regular expression syntax was a bit odd. each phrase in the expression represents a type of search feature. The following are some of the most common regular expressions that correspond to an example of how to use them:
Search for the header of a string using ^, such
False is returned because hello is not in the header of the string "I say hello world.
End of string
Search the end of a string with $, for example:
Returns false because bye is not at the end of the string "goodbye my friend.
Any single character
Search for any character and use a vertex (.), for example:
Returns false because the string to be searched does not contain any character. You can use curly brackets to tell the regular expression engine how many individual characters it will match. If I only want to match five characters, I can use ereg as follows:
The code above tells the regular expression engine to return true if at least five consecutive characters appear at the end of the string. we can also limit the number of consecutive characters:
In the above example, we have told the regular expression engine that our search string matches the expression. it must have 1 and 3 "a" characters at the end.
The above example will not return true. although there are three "a" characters in the search string, they are not at the end of the string. If we remove the ending string matching $ from the regular expression, this string is matched.
We can also tell the regular expression engine to match at least a certain number of characters in a row. if they exist, they can match more. We can do this:
Zero or multiple repeated characters
To tell the regular expression engine that a character may exist or can be repeated, we use the * character. Both examples here return true.
Even if the second example does not contain the "t" character, true is returned because * indicates that the character can appear but is not required. In fact, any common string mode will make the preceding ereg call return true, because the t character is optional.
One or more repeated characters
To tell the regular expression engine that a character must exist or can be repeated more than once, we use + characters, such
The following example also returns true:
Zero or duplicate characters
We can also tell the regular expression engine that a character must exist only once or not. We use? Character to do this job, just like
If we want to, we can completely delete c from the search string above. this expression will still return true .? It means that a c can appear anywhere in the search string, but it is not necessary.
Regular expression syntax (contd .)
To match the space characters in a search string, we use the predefined Posix
A regular expression is a specific format that can be used to identify the usage of a string in another string. Several programming languages, including...