PHP and regular expression _ PHP Tutorial

Source: Internet
Author: User
Tags ereg validation examples expression engine
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

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 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:
• 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.

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:
• 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)
Return true;
Return false;
Echo validateEmail (" ");
... Or use the following code:

Function validateEmail ($ email)
Return ereg ("^ [a-zA-Z] + @ [a-zA-Z] +. [a-zA-Z] + $", $ email );
Echo validateEmail (" ");

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

Returns true,

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 true,

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 true,

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 .)
Space character
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.