Blog from: swords
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 Visual Basic, Perl, javascript, and PHP, support regular expressions. I hope that at the end of this tutorial, Mitchell (by myself) you can apply some basic regular expressions in the PHP program. Regular Expressions are one of the odd features highlighted in a variety of programming languages, but since they seem to be a difficult concept, many developers put them in the corner, forget their existence.
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:
& #8226; ereg: The most common Regular Expression Function. ereg allows us to search for a string matching a regular expression. & #8226; ereg_replace: allows us to search for a string matching the Regular Expression and use a new string to replace all the places where this expression appears. & #8226; eregi: the effect is almost the same as that of ereg, but case insensitive. & #8226; eregi_replace: it has the same search-replace function as ereg_replace, but case-insensitive. & #8226; split: allows us to search for strings matching Regular Expressions and return matching results in the form of string sets. & #8226; spliti: The split function ignores case-insensitive versions.
|
Why use a regular expression?
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:
& #8226; are you writing custom functions to check form data (for example, one @ or one point in the email address )?
& #8226; 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 in upper case, or is it 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:
<? Php Function validateEmail ($ email) { $ HasAtSymbol = strpos ($ email ,"@"); $ HasDot = strpos ($ email ,"."); If ($ hasAtSymbol & $ hasDot) Return true; Else Return false; } Echo validateEmail ("mitchell@devarticles.com "); ?> ... Or use the following code:
<? Php Function validateEmail ($ email) { Return ereg ("^ [a-zA-Z] + @ [a-zA-Z] +. [a-zA-Z] + $", $ email ); } Echo validateEmail ("mitchell@devarticles.com "); ?>
|
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:
String Header
Search for the header of a string using ^, such
<? Php echo ereg ("^ hello", "hello world! ");?>
|
Returns true,
<? Php echo ereg ("^ hello", "I say hello world");?>
|
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:
<? Php echo ereg ("bye $", "goodbye");?>
|
Returns true,
<? Php echo ereg ("bye $", "goodbye my friend");?>
|
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:
<? Php echo ereg (".", "cat");?>
|
Returns true,
<? Php echo ereg (".", "") ;?>
|
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:
<? Php echo ereg (". $", "12345");?>
|
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:
<? Php echo ereg ("a $", "aaa");?>
|
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.
<? Php echo ereg ("a $", "aaab");?>
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:
<? Php echo ereg ("a $", "aaaa");?>
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.
<? Php echo ereg ("t *", "tom");?>
<? Php echo ereg ("t *", "fom");?>
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
<? Php echo ereg ("z +", "I like the zoo");?>
The following example also returns true:
<? Php echo ereg ("z +", "I like the zzzzzzzzoo! ");?>
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
<? Php echo ereg ("c? "," Cats are fuzzy ");?>
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 .)
Space Character
To match the space characters in a search string, we use the predefined Posix