Use regular expressions in PHP

Source: Internet
Author: User
Tags ereg
Use regular expression 1 in PHP and function 1) preg_match-for Regular Expression Matching. compatible with perl's regular syntax intpreg_match (string $ pattern, string $ subject [, array $ matches [, int $ flags] using regular expressions in PHP

1. Functions

1) preg_match-regular expression matching, compatible with perl's regular syntax

Int Preg_match(String $ Pattern, String $ Subject[, Array $ Matches[, Int $ Flags])
After the flag PREG_OFFSET_CAPTURE is added, it becomes a reverse match.

2) intEreg(String$ Pattern, String$ String[, Array& $ Regs])

Note: preg_match function ratioEreg ()Fast.

3) preg_grep-returns the array unit that matches the pattern.

Array preg_grep (string $ pattern, array $ input [, int $ flags])
Preg_grep () returns an array containing the units in the input array that match the given pattern.

Add the flag PREG_GREP_INVERT to reverse match.

(In the regular expression syntax ?! Peer)

For example


$ Subject = "4 | 43 | WINWORD. EXE | C: \ E ~ 1 \ iloveyou \ LOCALS ~ 1 \ Temp \~ DF39E9. tmp | EEWS | 1300763364 | ";
$ Pattern = '/\.(?! EXE | tmp). {3 }/';
Preg_match ($ pattern, $ subject, $ matches );
Print_r ($ matches );
?> 

2. how to make the regular expression case insensitive

SQL _regcase-generate a regular expression for matching with no size difference

$ Fl_array = preg_grep (SQL _regcase ("/^ (ASSZs)/"), $ array );

3. how to filter file names by file suffix

$ Logs = preg_grep (SQL _regcase ('/^ .*? \. (Tmp | ini | pip ).*? /'), $ Logstring, PREG_GREP_INVERT );

The file name with the suffix tmp | ini | txt is filtered out and case-insensitive.

 

Blog from dongzige

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.