Regular Expressions for PHP

Source: Internet
Author: User

Regular expressions

PHP supports a regular expression of two sets, a set of Pcre, Perl-compatible regular expressions, preg_-prefixed functions, and a regular expression of the POSIX extension syntax, using ereg_-prefixed functions, PCRE provides a slightly more efficient regular expression.

1. The regular expression syntax is the same as in Perl;

2. String Lookup

Preg_match (): Accepts three parameters, the first is pattern, the second is a specific string, the third parameter provides an array to save the matching content, where 0 is labeled as the whole, and 1 begins to represent the captured content;

Preg_match_all (): PHP does not have a pattern correction symbol: g, with this function substitution, complete the search function, the difference is that the function accepts the fourth optional parameter, when the fourth parameter is Preg_set_order, the value of subscript 0 begins to represent the captured content;

Preg_grep (): The second parameter is no longer a string, but an array containing a string that matches each element in the array, and returns a successful match;

Strstr (): Passes in two strings, searches for the second string in the first string, returns the part from the first string from the first searched position, and stristr () is a case-insensitive version;

Strpos (): Returns the position of the first occurrence of a string in another string, or False,strrpos () is the version to look up from the end if it is not found;

SUBSTR (): Returns a part of a string specifying the start and end positions;

3. String substitution:

Preg_replace (): The first parameter is the pattern, the second parameter is the need to replace the content, the third parameter is a string, the first two parameters can be a single pattern and a string, or an array containing multiple patterns, strings, the contents of the array should correspond to the same length;

Str_replace (): The first parameter is the lookup string, the second argument is the replacement string, the third parameter is the string content to be replaced, the fourth number of times to save the substitution, and str_ireplace () is the case-insensitive version;

4. String segmentation and linking:

Preg_split (): Pass in two parameters, the first parameter passes in the matching pattern, the second is the string that needs to be split, the third optional parameter limits the number of substrings to return, and the fourth parameter flag is any combination of the following tags:

Preg_split_no_empty: Make Preg_split () return only non-empty components;
Preg_split_delim_capture: Makes it possible to capture and return parenthesis expressions;
Preg_split_offset_capture: To return each occurrence of the matching results at the same time, return the offset of the owning string, the final most a two-tuple array;

Explode (): Pass in two strings, the first is a split tag string, the second is a split string, the third optional parameter limits the number of substrings returned, and the equivalent of Split ();

Implode (): The first argument is a link string, the second argument provides an array of strings, and returns the string after the string in the linked array using the link string;

5. In the above three basic operations, if the string function can be completed using the string function as far as possible, the regular expression as far as necessary to use the

Regular Expressions for PHP

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.