PHP and regular Expressions _php tutorial

Source: Internet
Author: User
Tags ereg validation examples expression engine
From: Swords's Blog

A regular expression is a specific format pattern that can be used to find out the use of a string in another string. Several programming languages, including Visual Basic,perl,javascript and PHP, support regular expressions, and hopefully at the end of this introductory guide, Mitchell (the author himself) will allow you to apply some basic regular expressions in your PHP program. Regular expressions are one of the quirky features that stand out in a variety of programming languages, but because they look like a difficult concept, many developers put them in a corner and forget about their existence.

Let's take a look at what regular expressions are, and why you're using them in a PHP program.

What is a regular expression?
What do you think of separating programs like BBEdit and Notepad from a nice old control-based text editor? Two supports text input, allowing you to save text to a file, but now the text editor also supports other features, including find – instead of tools, which makes editing a text file quite easy.
Regular expressions are similar, just a little bit better. Regular expressions can be thought of as an extremely advanced find-and-replace tool that lets us get out of the pain: no need to write custom data validation examples to check e-mail addresses or to confirm that phone numbers are formatted correctly, and so on.
One of the most common functions in any program is data validation, and PHP bundles some text-checking functions that allow us to match a string with a regular expression, confirm that there is a space, have a question mark, and so on.
What you don't know is that regular expressions can 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), and you ask yourself why you threw the regular expression into the corner for so long, ^_^.
PHP has two sets of functions to handle two types of regular expressions: Perl5 compatibility mode, and POSIX standard compatibility mode. In this article we will look at the Ereg function, working with a POSIX-compliant search expression. Although they are not as powerful as Perl5 patterns, they are a good way to learn about regular expressions. If you are interested in PHP-supported PERL5-compatible regular expressions, you can find some details about the Preg function on the php.net website.
PHP has six functions to handle regular expressions, all of which use a regular expression as their first parameter, listed as follows:
Ereg: The most commonly used regular expression function, Ereg allows us to search for a string that matches a regular expression.
Ereg_replace: Allows us to search for a string that matches a regular expression and replaces all occurrences of that expression with a new string.
eregi: And Ereg are almost the same effect, but ignoring the case.
eregi_replace: And ereg_replace have the same search-replace function, but ignore the case.
split: Allows us to search for a string that matches the regular expression and returns the matching result as a collection of strings.
The spliti:split function ignores the case version.


Why use regular expressions?

If you are constantly building different functions to check or manipulate part of a string, now you may want to discard all of these functions and replace them with regular expressions. If you answer "yes" to the following questions, then you must consider using regular expressions:
• Are you writing custom functions to check the form data (e.g. one @, one point in the e-mail address)?
• Do you write custom functions that loop through each character in a string, and if the character matches a particular feature (for example, if it is uppercase, or if it is a space), then replace it?
In addition to being uncomfortable with string checking and manipulation methods, if you don't write code efficiently, the above two will slow down your program. Do you prefer to check an e-mail address with the following code:
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:

function Validateemail ($email)
{
Return Ereg ("^[a-za-z]+@[a-za-z]+.[ a-za-z]+$ ", $email);
}
Echo validateemail ("mitchell@devarticles.com");
?>

To be sure, the first function is easier, and looks like a good structure. But wouldn't it be easier if we checked the function with the next version of the email address above?
The second function shown above uses only regular expressions, including a call to the Ereg function. The Ereg function returns TRUE or FALSE to declare whether its string argument matches the regular expression.
Many programmers shun regular expressions only because they (in some cases) are slower than other text-processing methods. The reason that regular expressions can be slow is because they involve copying and pasting strings in memory because each new part of the regular expression corresponds to a string. However, from my experience with regular expressions, unless you run a complex regular expression on hundreds of lines in the text, the performance flaws are negligible and rarely occurs when the regular expression is used as the input data checker. "


Regular expression syntax
Before you can match a string to a regular expression, you must first establish a regular expression. At first, the syntax of the regular expression is a bit odd, and each phrase in the expression represents a type of search feature. The following are some of the most common regular expressions, and they all correspond to an example of how to use it:

String header
Search for the head of a string, with ^, for example


will return true, but


will return false because Hello is not in the header of the string "I say hello World".
String trailing

Search for the tail of the string, with $, for example:


will return true, but


False is returned because bye is not at the tail of the string "Goodbye my Friend".

Any single character
Search for any character, using dots (.), for example:


will return true, but


will return false because our search string does not contain characters. You can use curly braces to tell the regular expression engine how many individual characters it will match. If I only want to match 5 characters, I can use ereg like this:


The above code tells the regular expression engine to return true if and only if at least 5 consecutive characters appear at the end of a string. We can also limit the number of characters that appear consecutively:


In the example above, we have already told the regular expression engine that our search string matches the expression, which must have a "a" character between 1 and 3 at the tail.

The above example will not return true, although there are three "a" characters in the search string, but they are not at the tail of the string. If we take the ending string match $ from the regular expression, then the string is matched.
We can also tell the regular expression engine to match at least a certain number of words noon a line, if they exist, can match more. We can do this:


0 or more repeating characters
In order to tell the regular expression engine that a character may exist, it can also be repeated, we use the * character. The two examples here will return true.


Even though the second example does not contain the character "T", it still returns ture because the * indicates that the character can appear, but not must appear. In fact, any normal string pattern will cause the above Ereg call to return True, because the T character is optional.

One or more repeating characters
To tell the regular expression engine that a character must be present or repeated more than once, we use the + character, like

The following example also returns true:


0 or one repeating character
We can also tell the regular expression engine that a character must be or exist only once, or not. We use the word character to do the work, just like

If we want to, we can completely remove C from the search string above, and the expression will still return true. It means that a C can appear anywhere in the search string, but it is not required.

Regular expression syntax (contd.)
Space character
In order to match the space character in a search string, we use the pre-defined POSIX

http://www.bkjia.com/PHPjc/532679.html www.bkjia.com true http://www.bkjia.com/PHPjc/532679.html techarticle From: Swords Blog A regular expression is a specific format pattern that can be used to find out the use of a string in another string. Several programming languages, including ...

  • Related Article

    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: info-contact@alibabacloud.com 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.