If you constantly establish a different function to check or operate a part of the string, now you may want to give up all these functions, replaced by the regular expression. If you answer the following questions, then you must consider using the regular expression: Are you writing some custom functions to check form data (such as one in the email address), one point) ? Do you write some custom functions, loop each character in a string, if this character matches a specific feature (such as it is capital, or it is a space), then replace it? In addition to being Uncomfortable string checks and operation methods, if there is no efficient writing code, the above two will slow your program slow down. Are you more preferred to check an email address with the following code:
Function ValidateEmail ($ 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");?> You can be sure that the first function is relatively easy, and it looks good. But if we use the next version of the email address to check the function is not easier? The second function shown above only used regular expressions, including a call to EREG functions. EREG function returns true or false to declare whether its string parameter matches the regular expression. Many programmers avoid regular expressions, just because they (in some cases) is slower than other text processing methods. Regular expression may slowly because they involve copying and paste strings in memory because each new part of the regular expression corresponds to a string. However, from my experience of regular expressions, unless you run a complex regular expression in hundreds of rows in text, performance defects can be ignored, when the regular expression is used as input data inspection tools This is rarely happening. Regular expression syntax You must establish a regular expression before you can match a string to the regular expression. At the beginning, the symmetric expression of the regular expression is a bit weird. Each phrase in the expression represents a type of search characteristics. The following is some of the most ordinary regular expressions, and there should be an example of how to use it: the string header search a string head, ^, for example
Will return True, but
False will return because hello is not in the head of the string "I Say Hello World". String tail search string tail, with $, for example:
Will return True, but
False will return because Bye is not at the end of the string "Goodbye My Friend". Any single character search for any character, use points (.), For example:
Will return True, but
Will return false because our search string does not contain characters. You can use the curly brackets to tell the regular expression engine to match how many individual characters. If I just want to match 5 characters, I can use EREG:
The above code tells the regular expression engine to return True when the end of the string occurs only when at least 5 consecutive characters appear. We can also limit the number of characters that appear continuous: In the above example, we have told the regular Expression engine, our search string to match expressions, which must have an introduction to 1 and 3 "A" characters at the end.
The above example will not return True, although there are three "A" characters in search strings, but they are not at the end of the string. If we match the end string match $ from the regular expression, then this string is matched. We can also tell the regular expression engine to match at least a certain number of characters, if they exist, can match more. We can do this:
Zero or multiple repeat characters In order to tell the regular expression engine, the expression engine may exist, or repeat, we use * characters. The two examples here will return True.
Even if the second example does not contain "T" characters, but still returns Ture, because * indicates that the character can appear, but it is not necessary. In fact, any normal string mode returns True above EREG call because 't' characters are optional. One or more repeating characters must be existed in order to tell the regular expression engine, or repeated more than once , We use characters, like
The following example will also return True:
Zero or a repeating character we can also tell the regular expression engine, a character must or only once, or not. We use the characters to do this job, just like
If we prefer, we can completely remove 'c' from the above search string. This expression will still return True. '?' What is the meaning of 'c' can appear anywhere in the search string, but not necessary of. Regular expression syntax (continued) space character In order to match a space character in a search string, we use predefined POSIX classes, [[: space]]. Gu brackets indicate the correlation between continuous characters, ": Space:" Yes The actually needed class (in this case, it is any blank character). Blank includes Tab characters, new row characters, blank characters. Or, if the search string must contain only one space, instead of a Tab or new row character, you can use a space character (""). In most cases, I tend to use ": Space:", because this means that my intent is not just a single space character, this is easy to be ignored. Here are some POSIX-standard predefined classes, there are some POSIX-standard predefined classes we can act as part of the regular expression, including [: alnum:], [: Digit:], [: Lower:], and more. A complete list can be viewed here to match a single blank character like this:
We can also tell the regular expression engine to match the regular expression engine by using the characters after the expression, the expression engine matches no blank or a blank.
Mode packet-related patterns can be separated in square brackets. It is easy to specify only one lowercase letter or a column of uppercase letters to search for a part of the string.
// Require from the first to the last one is lowercase letters echo EREG ("^ [a-z] $", "johndoe"); // Return true?> Or like
// Require from the first to the last one is uppercase letters EREG ("^ [AZ] $", "johndoe"); // Return true ??> We can also tell the regular expression engine, we hope or Small write letters, or uppercase letters. We can do it if we combine [A-Z] and [A-Z] mode.
In the above example, if we match "john doe", not "johndoe", will be very meaningful. We do this with the following regular expression: ^ [a-za-z] [[: space:]] {1} [A-ZA-Z] $ easy to search for a digital string word group not only Search mode can be packet, we can also group related search words with parentheses.
In the above example, we have a string head character, follow "John" or "Jane", at least one other characters, then a string tail character. and so…
... Will also match our search mode special characters because some characters should be used in a clear grouping or syntax in a search mode, like parentheses in (John | Jane), we need to tell the regular expression engine Shield these characters and processes them into part of the search string instead of searching for a part of the search expression. The method we use is called "character escape", which involves adding any "dedicated symbol" to a backslash. So, for example, if I want to include '|' in my search, then I can do this.
Here is just a small amount of characters you want to escape, you must escape ^, $, (,),., [, |, *,, , / And {. I hope that you have a little bit of a little bit to the regular expression. Now let's take two examples of a string in the data with a regular expression. Regular expression example 1 Example 1 Let's make quite simple to do the first example, check a standard URL. A standard URL (no port number), there are three parts: [protocol]: // [domain name] Let us Start with the protocol part of the matching URL, and let it only use HTTP or FTP. We can do this with the following regular expression: ^ (http | ftp) ^ character refers to the head of strings, using parentheses Will HTTP and FTP, and use "or" symbol (|) to separate them, we tell the regular expression engine HTTP and FTPs must start at the beginning of the string. A domain name is usually composed of www.somesite.com, but you can choose not to choose WWW portion. For example, we only allow .com, .NET, and .org's domain names in consideration. It is best to indicate the domain name part of the regular expression as follows: (www /.)? /. (Com | net | org) $ put all things together, our regular expression can be used as Check a domain name, such as:
Function isvaliddomain ($ DomainName) {Return EREG ("^ (http | ftp): // (www /.)?. /. (com | net | org) $", $ domainname);} // true (True Echo isvaliddomain ("http://www.somesite.com"); // true ("ftp://somesite.com"); // false Echo isvaliddMain ("ftp: / /www.somesite.fr" "(false) echo isvaliddomain (" www.somesite.com ");?> Example I live in Australian Sydney, let us check a typical Australian international phone number. The format of the Australian international phone number is as follows: 61x XXXX-XXXX The first X is the area code, and the other is a telephone number. Check the phone number with ' 61' and follow the area code between 2 to 9, we use the following regular expression: ^ / 61 [2-9] [[: Space:]] Note, above The search mode uses' 'characters' escape to facilitate containing in the search, not to be interpreted as a regular expression. [2-9] Tell the regular expression engine We need to include numbers between 2 to 9. [[: Space:]] Class tells the regular expression expects there is a blank here. Here is the remaining search mode of the phone number: [0-9] {4} - [0-9] {4} $ This is not unusual place, we just tell the regular expression engine phone number available, it It must be a combination of 4 numbers, followed by a connecting, followed by another 4 digits, and then a string tail character. Put your full regular expression, put it into a function, we can use code to check some Australian international phone numbers: Function isvalidphone ($ phonenum) {echo EREG ("^ / 61 [2-9] [[: Space:]] [0-9] {4} - [0-9] {4} $ ", $ phonenum);} // true (TRUE) Echo isvalidphone (" 619 0000-0000 "); // (False) Echo isvalidphone (" 61 000000"); // False Echo isvalidphone (" 611 000000");?>>>>>