PHP Learning practiced Hand (16)

Source: Internet
Author: User

Perl-compatible regular expressions

First, Function:

Preg_match (): Indicates whether the pattern matches the string and returns 1 if a match is found, otherwise 0

Preg_match_all (): Found all matches

Preg_replace (): Implementing the Find and Replace function

Second, the simple mode:
1. Literal: is a value that is written in exactly the same way as it is interpreted. For example, pattern a will match the letter A,ab will match AB and so on.

    1. Delimiter: can be any character except alphanumeric or backslash, you must use the same character to mark the beginning and end of the pattern. Usually use a forward slash (/)

    2. Metacharacters:

    3. Quantifiers:

    4. Common character Categories:


      Third, Practice:

      1. Single Match--pcre.php

<htmllang="en"><head><metacharset="UTF-8"><title>Testing PCRE 
     title
 > 
     Head
 ><body> 
      # Script 14.1-pcre.phpif($_server[' Request_method '] = = ' POST '            { $pattern = Trim ($_post[' pattern ']);            $subject = Trim ($_post[' subject ']); Echo "

The result of checking
$pattern
Aganist
$subject
Is

" ; if (Preg_match ($pattern, $subject)) { echo' true!

'; }Else{ echo' false!

'; }} ?>

<formAction="pcre.php"method="POST"><p>Regular Expression Pattern:<inputtype="text"name="pattern"value= " "size="Max">(Include the delimiters) P ><p>Test Subject:<inputtype="text"name="Subject"value =" "size=" /> " P ><inputtype="Submit"name= "submit"value="test!" > Form > Body > HTML >

--e-mail verification: E-mail starts with letters, numbers or underscores, and a period (.) or dash.

2. All matching--matches.php

<htmllang="en"><head><metacharset="UTF-8"><title>Testing PCRE 
     title
 > 
     Head
 ><body>
  
     # Script 14.1-pcre.php if        ($_server[' Request_method '] = = ' POST ')            { $pattern = Trim ($_post[' pattern ')];            $subject = Trim ($_post[' subject ']); Echo "

The result of checking
$pattern
Aganist
$subject
Is

"; if (Preg_match_all ($pattern, $subject, $matches)) { echo' true!

'; Echo '

'. Print_r ($matches,1).'
'; }Else{Echo' false!

'

; } }?><formAction="matches.php"method="POST"><p>Regular Expression Pattern:<inputtype="text"name="pattern"value =" "size="> "(Include the delimiters) P ><p>Test Subject:<textareaname="Subject"rows="5"cols= "> " if(isset($subject)) echo htmlentities ($subject); ?> textarea > P ><inputtype="Submit"name="Submit"value ="test!" > Form > Body > HTML >

3. Match and replace--replace.php

<htmllang="en"><head><metacharset="UTF-8"><title>Testing PCRE 
         title
 > 
         Head
 ><body>
        # Script 14.1-pcre.phpif($_server[' Request_method '] ==' POST ')        {$pattern= Trim ($_post[' pattern ']);$subject= Trim ($_post[' Subject ']);$replace= Trim ($_post[' Replace ']);Echo$subject;Echo"

The result of replacing
$pattern
With
$replace
Inch
$subject

"; if (Preg_match ($pattern, $subject)) { echo preg_replace ($pattern, $replace, $subject). '

'; }Else{Echo' The pattern is not found!

'

; } }?><formAction="replace.php"method="POST"> <p>Regular Expression Pattern:<inputtype="text"name="pattern"value=" " Size="Max">(Include the delimiters) P ><p>Replacement:<inputtype="text"name="Replace"value =" "size="> " P ><p>Test Subject:<textareaname="Subject"rows="5"cols="Max" > if(isset($subject)) echo htmlentities ($subject ); ?> textarea > P ><inputtype="Submit"name="Submit"value ="test!" > Form > Body > HTML >

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP learning practiced hand (16), including aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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