Preg series functions in PHP

Source: Internet
Author: User
Tags preg expression engine

    • Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])

Returns an array if subject is an array, otherwise returns a string. If an error occurs, NULL is returned. Modifier \e is deprecated, use preg_replace_callback

$pattern, $replacement, $subject can be both arrays and strings.

The $replacement indicates a back reference with \\n or $n. To use backslashes in $replacement, you must use 4 ("\\\\": Because this is the first PHP string, after escaping, it is two, then after the regular expression engine is considered a text backslash)

A back reference followed by another number requires the use of the ${n}m

$limit represents the maximum number of times each pattern is replaced on each subject
Number of $count replacements

    • Mixed Preg_replace_callback (mixed $pattern, callable $callback, mixed $subject [, int $limit =-1 [, int & $count] ] )

The behavior of this function, in addition to specifying a callback substitution replacement for the calculation of the replacement string, is equivalent to Preg_replace ().

<?PHP//increase the year in the text by one year.$text ="April Fools Day is 04/01/2002\n"; $text.="Last Christmas was 12/24/2001\n";//callback functionfunction Next_year ($matches) {//usually: $matches [0] is a complete match//$matches [1] is the first capturing subgroup of a match//etc.  return$matches [1]. ($matches [2]+1);} Echo Preg_replace_callback ("| (\d{2}/\d{2}/) (\d{4}) |",            "Next_year", $text);?>April Fools Day is Geneva/ on/2003Last Christmas was A/ -/2002

    • int Preg_match (string $pattern, String $subject [, Array & $matches [, int $flags = 0 [, int $offset = 0]])

Searches for a match between the subject and the regular expression given by the pattern .

Preg_match () returns pattern the number of matches . Its value will be 0 times (mismatched) or 1 times, because Preg_match () will stop the search after the first match. Preg_match_all () differs from this, and it searches subject until the end is reached. If an error occurs , Preg_match () returns FALSE .

$matches
If the parameter matches is supplied, 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.

$flags
Flags can be set to the following tag values:

Preg_offset_capture
If this token is passed, a 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. The optional parameter offset is used to specify a search starting at a location from the target string (in bytes).

<?  "abcdef"'/^def/';p reg_match ($ Pattern, substr ($subject,3), $matches, preg_offset_capture);p Rint_r ($matches);? >Array (    [0] = = array        (            [0] = def            [  10        ))

    • int Preg_match_all (string $pattern, String $subject [, Array & $matches [, int $flags = Preg_pattern_order [, int $o Ffset = 0]])

Returns the complete number of matches (possibly 0), or False if an error occurs.

Matches
multidimensional arrays, which output all matching results as output parameters, array ordering is specified by flags.

Flags

PREG_PATTERN_ORDER:结果排序为$matches[0]保存完整模式的所有匹配, $matches[1] 保存第一个子组的所有匹配,以此类推。

<?Phppreg_match_all ("|<[^>]+> (. *) </[^>]+>| U",    "<b>example: </b><div align=left>this is a test</div>",    $ out, Preg_pattern_order); echo $ out[0][0] .", ". $ out[0][1] ."\ n"; echo $ out[1][0] .", ". $ out[1][1] ."\ n";><b>example: </b>, <div align=left> This  isA test</div>Example:, This  isA test

PREG_SET_ORDER

The result is sorted to $matches[0] include all occurrences of the first match (including subgroups), the $matches[1] array that contains all matches (including subgroups) that were matched to the second match, and so on.

<?Phppreg_match_all ("|<[^>]+> (. *) </[^>]+>| U",    "<b>example: </b><div align=\ "Left\" >this is a test</div>",    $ out, Preg_set_order); echo $ out[0][0] .", ". $ out[0][1] ."\ n"; echo $ out[1][0] .", ". $ out[1][1] ."\ n";? ><b>example: </b>, Example:<div align=" Left"> This  isa test</div> This  isA test

PREG_OFFSET_CAPTURE

If this token is passed, each found match is returned with an offset to its relative target string. Note that this changes matches each of the matching result string elements so that it becomes a 0 element that matches the result string, and the 1 element is the offset of the matching result string in subject .

    • Array preg_grep ( string $pattern , array $input [, int $flags = 0])

Returns an array of elements in the given array that input pattern match the pattern.

flags

If set to PREG_GREP_INVERT , this function returns an array of elements in the input array that pattern do not match the given pattern.

$array = Array (10.2,+,'ab',3.4,5.5= preg _grep ("/^ (\d+) \.\d+$/", $array,1);p rint_r ($FL _array); Array ([1 ] [2] = AB)

    • String Preg_quote (String $str [, String $delimiter = NULL])

Escaping regular expression characters

Preg_quote () requires str a parameter and adds a backslash to the character in each of the regular expression syntax. This is typically used when you have some run-time strings that need to be matched as regular expressions.

The regular expression special characters are: . \ + *? [ ^ ] $ ( ) { } = ! < > | : -

$textbody ="This was *very* difficult to find."; $word="*very*"; $textbody= Preg_replace ("/". Preg_quote ($word)."/",                          "<i>". $word."</i>", $textbody); Echo $textbody; this book is*very* difficult to find.

Preg series functions in 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.