PHP regular expressions

Source: Internet
Author: User
Tags character set control characters html tags numeric script tag

PHP regular expressions

1. Write a function to verify that the email format is correct.

Function checkemail ($ email ){

Echo preg_match ('/^ [w] + @ [w] + [.] ([w] +) $/', $ email )? 'Email format is correct ': 'Incorrect email format ';

        }

Pair

2. Write a regular expression to overwrite all JS/VBS scripts on the web page (that is, remove the script tag and its content ):

Preg_replace ("/<script [^>]. *?>. *? </Script>/si "," newinfo ", $ script );

3. Write the regular expression that matches the URL.

/^ [A-zA-z] +: // (w + (-w +) *) (. (w + (-w + )*))*(? S *)? /Powerful


/^ Http: // www. ([w] +). ([w] +) $/weak
  

4. Verify the regular expression of the 11-digit mobile phone number starting with 139

Preg_match (/^ 139 [0-9] {8} $ /)

Pair

4. Write at least one regular expression for verifying 11 mobile phone numbers.

Preg_match ("/^ 13 [0-9] {1} [0-9] {8} $ /")

Pair


5. Write a regular expression to express the integers and floating-point numbers in PHP.

/^ (0 | [1-9] d *) ([.] 5 )? $/

Pair


6. Use a regular expression to write a function to verify that the IP address format is correct?


/A ([0-9]? [0-9]) | (1 [0-9] {2}) | (2 [0-4] [0-9]) | (25 [0-5]).) {3} ([0-9]? [0-9]) | 1 [0-9] {2}) | (2 [0-4] [0-9]) | (25 [0-5]) Z/

Error


/^ (25 [0-5] | 2 [0-4] d | 1d {2} | d {1, 2 }).) {3} (25 [0-5] | 2 [0-4] d | 1d {2} | d {1, 2}) $/

Pair


7. Write a regular expression to match the image address in the mark of an HTML file.


/ "'s] *)/I pair


8. Regular expressions matching HTML tags


/<(. *)>. * </1> | <(. *)/>/


9. Match the Chinese phone number

(D {3}-| d {4 }-)? (D {8} | d {7 })?


10. Chinese, English, numbers, and underscores

/^ [U4e00-u9fa5_a-zA-Z0-9] + $/Dizzy does not seem to support Chinese

English, numbers, and underlines/^ [_ a-zA-Z0-9] + $/


11./^ [A-Za-z] + $ // A string consisting of 26 letters

"^ D + $" // non-negative integer (positive integer + 0)
"^ [0-9] * [1-9] [0-9] * $" // positive integer
"^ (-D +) | (0 +) $" // non-positive integer (negative integer + 0)
"^-[0-9] * [1-9] [0-9] * $" // negative integer
"^ -? D + $ "// integer
"^ D + (. d + )? $ "// Non-negative floating point number (positive floating point number + 0)
"^ ([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// positive floating point number
"^ (-D + (. d + )?) | (0 + (. 0 + )?)) $ "// Non-positive floating point number (negative floating point number + 0)
"^ (-([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// negative floating point number
"^ (-? D +) (. d + )? $ "// Floating point number
"^ [A-Za-z] + $" // A string consisting of 26 English letters
"^ [A-Z] + $" // a string consisting of 26 uppercase letters
"^ [A-z] + $" // a string consisting of 26 lowercase letters
"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters
"^ W + $" // a string consisting of digits, 26 letters, or underscores
"^ [W-] + (. [w-] +) * @ [w-] + (. [w-] +) + $" // email address
"^ [A-zA-z] +: // (w + (-w +) *) (. (w + (-w + )*))*(? S *)? $ "// Url
/^ (D {2} | d {4})-(0 ([1-9] {1}) | (1 [1 | 2]) -([0-2] ([1-9] {1}) | (3 [0 | 1]) $ // year-month-day
/^ (0 ([1-9] {1}) | (1 [1 | 2]) /([0-2] ([1-9] {1}) | (3 [0 | 1]) /(d {2} | d {4}) $ // month/day/year
"^ ([W-.] +) @ ([0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }.) | ([w-] + .) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (]?) $ "// Emil
/^ (+? [0-9] {2, 4}-[0-9] {3, 4}-) | ([0-9] {3, 4 }-))? ([0-9] {7,8}) (-[0-9] + )? $ // Phone number
"^ (D {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]) $ "// IP address
Regular Expression Matching Chinese characters: [1-?]
Match double byte characters (including Chinese characters): [^ x00-xff]
Regular expression for matching empty rows: n [s |] * r
Regular expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/
Regular expression matching the first and last Spaces: (^ s *) | (s * $)
Regular Expression Matching the Email address: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*
Regular Expression Matching URL: ^ [a-zA-z] +: // (\ w + (-\ w + )*)(\. (\ w + (-\ w + )*))*(\? \ S *)? $
Match account validity (starting with a letter, may be 5-16 bytes, may be an alphanumeric underline): ^ [a-zA-Z] [a-zA-Z0-9 _] {} $
Match Chinese phone number: (d {3}-| d {4 }-)? (D {8} | d {7 })?
Match Tencent QQ number: ^ [1-9] * [1-9] [0-9] * $
Metacharacters and their behavior in the context of regular expressions:
Mark the next character as a special character, a literal character, a back reference, or an octal escape character.
^ Matches the start position of the input string. If the Multiline attribute of the RegExp object is set, ^ matches the position after 'N' or 'R.
$ Matches the end position of the input string. If the Multiline attribute of the RegExp object is set, $ also matches the location before 'n' or 'R.
* Matches the previous subexpression zero or multiple times.
+ Match the previous subexpression once or multiple times. + Is equivalent to {1 ,}.
? Match the previous subexpression zero or one time .? It is equivalent to {0, 1 }.
{N} n is a non-negative integer that matches the specified n times.
{N ,}n is a non-negative integer and matches at least n times.
Both {n, m} m and n are non-negative integers, where n <= m. Match at least n times and at most m times. There must be no space between a comma and two numbers.
? When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible.
. Match any single character except "n. To match any character including 'n', use a pattern like '[. n.
(Pattern) matches pattern and obtains this match.
(? : Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use.
(? = Pattern) forward pre-query: matches the search string at the beginning of any string that matches pattern. This is a non-get match, that is, the match does not need to be obtained for future use.
(?! Pattern) negative pre-query, and (? = Pattern).
X | y matches x or y.
[Xyz] character set combination.
[^ Xyz] combination of negative character sets.
[A-z] character range, matching any character in the specified range.
[^ A-z] the negative character range matches any character that is not within the specified range.
B matches a word boundary, that is, the position between a word and a space.
B matches non-word boundaries.
Cx matches the control characters specified by x.
D matches a numeric character. It is equivalent to [0-9].
D. Match a non-numeric character. It is equivalent to [^ 0-9].
F matches a form feed. It is equivalent to x0c and cL.
N matches a linefeed. It is equivalent to x0a and cJ.
R matches a carriage return. It is equivalent to x0d and cM.
S matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [fnrtv].
S matches any non-blank characters. It is equivalent to [^ fnrtv].
T matches a tab. It is equivalent to x09 and cI.
V matches a vertical tab. It is equivalent to x0b and cK.
W matches any word characters that contain underscores. It is equivalent to '[A-Za-z0-9 _]'.
W matches any non-word characters. It is equivalent to '[^ A-Za-z0-9 _]'.
Xn matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be determined by the length of two numbers.
Num matches num, where num is a positive integer. References to the obtained matching.
N identifies an octal escape value or a backward reference. If n contains at least n obtained subexpressions, n is a back reference. Otherwise, if n is an octal digit (0-7), n is an octal escape value.
Nm identifies an octal escape value or a backward reference. If there are at least is preceded by at least nm obtained subexpressions before nm, then nm is backward reference. If at least n records are obtained before nm, n is a backward reference followed by m. If none of the preceding conditions are met, if n and m are octal numbers (0-7), nm matches the octal escape value nm.
If n is an octal digit (0-3) and m and l are octal numerals (0-7), nml matches the octal escape value nml.
Un matches n, where n is a Unicode character represented by four hexadecimal numbers.
Regular Expression Matching Chinese characters: [1-?]
Match double byte characters (including Chinese characters): [^ x00-xff]
Regular expression for matching empty rows: n [s |] * r
Regular expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/
Regular expression matching the first and last Spaces: (^ s *) | (s * $)
Regular Expression Matching the Email address: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*
The regular expression matching the URL: http: // ([w-] +.) + [w-] + (/[w -./? % & =] *)?
Use regular expressions to restrict text box input in a webpage form:
Use a regular expression to restrict Chinese characters only: onkeyup = "val = val. replace (/[^ 1-?] /G, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ 1-?] /G ,''))"
Use a regular expression to limit that only the full-width characters can be entered: onkeyup = "val = val. replace (/[^? -?] /G, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^? -?] /G ,''))"
Use a regular expression to limit that only numbers can be entered: onkeyup = "val = val. replace (/[^ d]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ d]/g ,''))"
Only numbers and English characters can be entered with a regular expression: onkeyup = "val = val. replace (/[W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ d]/g ,''))"

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.