PHP Basics-Regular Expressions

Source: Internet
Author: User
Tags deprecated php basics expression engine

A very important way of handling strings in PHP is regular expressions, which is also a method in other languages, because regular expressions are really too powerful for string processing, so here's a summary, remember the first time I touched PHP, Still thinking about the use of JavaScript in the PHP page regular expression, later was reminded that the original PHP has its own regular expression, it was really embarrassing.

The following are some of the main functions of regular expressions in PHP:

int   preg_match   ( string   $pattern  , string  $ Subject  [, array  & $matches  [, int   $flags  = 0  [,int   $offset  = 0  ]])

preg_match () returns the number of occurrences of pattern. Its value will be 0 times (mismatched) or 1 times, because Preg_match () will stop the search after the first match.
Searches for a match in subject with the regular expression given by the pattern.

Pattern to search for, string type.

Subject the input string.

Matches if a parameter matches is provided, it will be populated as search results. $matches [0] will contain the text that the full pattern matches to,$matches [1] will contain the text to which the first capturing subgroup matches, and so on.

The flagsflags can be set to the following tag values: Preg_offset_capture If this token is passed, the string offset (relative to the target string) is appended to each occurrence of the match returned. Note: This changes the array populated to the matches parameter so that each of its elements becomes a string that is matched to by the No. 0 element, and the 1th element is the offset of the matching string in the target string subject.

Offset typically, the search starts at the beginning of the target string. Optional parameter offset is used to specify a search starting from an unknown of the target string (in bytes)


Preg_replace ( $pattern ,  $replacement  ,  $subject  [,  int $limit =-1 [,  int & $count  ]] )

pattern
the mode to search for. Can make a string or array of strings.
You can use somePcre modifier, including the deprecated 'e' (preg_replace_eval), which can be specified for this function.

Replacement
a string or array of strings to replace. If this argument is a string, andpatternis an array, all patterns are replaced with this string. Ifpatternand theReplacementare arrays, eachpatternUseReplacementto replace the corresponding element in the IfReplacementthe element ratio inpatternin the small, more out ofpatternReplace with an empty string.
Replacementyou can include a back reference in the\N or (PHP 4.0.4 or more available)$N , the latter is preferred syntactically. Each such reference will be matched to the firstNCaptured sub-groups to capture the text substitution. Nit could be 0-99,\\0and the $represents the complete pattern-matching text. The ordinal count of the capturing subgroup is: The opening parenthesis representing the capturing subgroup is left to right, starting at 1. If you want toReplacementuse a backslash in the 4 ("\\\\", because this is the first PHP string, after escaping, is two, and then after the regular expression engine is considered a text backslash).
when working in replacement mode and the back reference is followed by a second number (for example, adding a text number immediately after a matching pattern), you cannot use\\1This syntax is used to describe the back reference. For example,\\11will makepreg_replace ()can't understand what you're hoping for is a\\1the back reference is immediately followed by a text1, or a\\11the back reference is not followed by anything. In this case the solution is to use the\${1}1. This creates a stand-alone $a back reference, an independent source1. when using a deprecatedemodifier, this function escapes some characters (that is:',",\and NULL) and then replace the back reference. When these are done, make sure that there are no single or double quotation marks due to syntax errors after parsing the back reference (for example:' strlen (\ ' $1\ ') +strlen ("$") '). Ensure that PHP-compliantString syntaxand conforms to the eval syntax. Because after the substitution is complete, the engine evaluates the resulting string as PHP code using the Eval method and returns the return value as a string that will be substituted for the final participation.

subject
string or array of strings to search for and replace.
subject Span style= "font-family: ' Microsoft Yahei '; font-size:18px ">subject  , And the return value will also be an array.

Limit
each pattern in each subject the maximum number of times to replace on the The default is -1(infinity).

Count
if specified, it will be populated with the number of replacements completed.

common meta characters include: "+", "*", and "?", {n,m}. where the "+" metacharacters stipulate that their leading characters must appear one or more times in the target object, the "*" metacharacters stipulate that their leading characters must appear 0 or more times in the target object, and "?" Metacharacters specify that their leading objects must appear 0 or more times in the target object, {n,m} white Oh is a leading object that appears at least n times, at most, M times.
\s: Used to match a single space character, including Tab key and line break;
\s: Used to match all characters except a single space character;
\d: Used to match numbers from 0 to 9;
\w: Used to match letters, numbers, or underscore characters;
\w: Used to match all characters that do not match the \w;
.: Used to match all characters except line break.

commonly used locators include: "^", "$", "\b", and "\b". Where the "^" locator specifies that the matching pattern must be at the beginning of the target string, the "$" locator specifies that the matching pattern must be at the end of the target object, and the \b Locator specifies that the matching pattern must be one of the two boundaries at the beginning or end of the target string, and "\b" A locator specifies that the matching object must be within the first and end two boundaries of the target string, that is, the matching object cannot be either the beginning of the target string or the end of the target string.
[A-z] matches a lowercase letter A to z,[0-9], matching numbers 0 through 9.

The escape character \ is used to match those meta characters, locators, and so on.


PHP Basics-Regular Expressions

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.