Reverse reference of PHP regular expressions and sub-schema PHP preg_replace application

Source: Internet
Author: User

Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])

Function

Searches the subject for a match in pattern mode and replaces it with replacement. If limit is specified, only the limit match is replaced, and if the limit is omitted or the value is-1, all occurrences are replaced.
Replacement can contain reverse references in the form of \\n or $n, and n can represent 0 to 99,\\n text that matches the pattern nth child pattern, and \\0 represents text that matches the entire pattern.

Sub-mode

$pattern the regular expression enclosed in parentheses in the argument, the number of child patterns is the number of parentheses from left to right. (Pattern is mode)

Example

Requirement: The OT function is changed from OT (a) to OT (A, b), plus a parameter

<?php

$str = <<<eod
_article_source=ot ('. Div ' (posid (' Bd_nav ') >0) [1#].a (poshtml ('/b* ') >0) [#] ')
; _article_author=ot ('. Div (Posclass (' Doc_top p9 ') >0) [1#].a (Posclass (' B ') >0) [#] ')

EOD;

$reg = '/(=ot\ (. +) (\) [\r,\n])/';

$replacement = ' $1,n$2 ';
Echo preg_replace ($reg, $replacement, $STR);
?>

Reverse reference of PHP regular expressions and sub-schema PHP preg_replace application

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.