In preg_replace, how does one reference a named capture group with a replacement parameter?

Source: Internet
Author: User
A general capture group can use 1N as the table name. if it uses P & amp; lt; xxx & amp; gt;, what is the name of the capture result in The Replacement parameter? {Code ...} it seems that k & amp; lt; xxx & amp; gt; is used directly. Otherwise, the average capture group can use \ 0 \ 1 \ N as the table name. if P is used In this way, what is used to represent the capture result in The Replacement parameter?

  
   ";  $text.="";  $text.="

some

"; $out=preg_replace("/<(?P span|p)[^>]+>(.+)<\/(\k )>/","[\P= TAG]\\1[\/\k ]",$text); print_r( $out);

It seems that k is used directly. This is not the case.

Reply content:

For a general capture group, \ 0 \ 1 \ N can be used as the table name. In this way, what is used to represent the capture result in The Replacement parameter?

  
   ";  $text.="";  $text.="

some

"; $out=preg_replace("/<(?P span|p)[^>]+>(.+)<\/(\k )>/","[\P= TAG]\\1[\/\k ]",$text); print_r( $out);

It seems that k is used directly. This is not the case.

Http://php.net/manual/en/function.preg-replace.php

Replacement may contain references of the form \ n or (since PHP
4.0.4) $ n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n 'th
Parenthesized pattern. n can be from 0 to 99, and \ 0 or $0 refers
The text matched by the whole pattern. Opening parentheses are counted
From left to right (starting from 1) to obtain the number of
Capturing subpattern. To use backslash in replacement, it must be
Doubled ("\" PHP string ).

When working with a replacement pattern where a backreference is
Immediately followed by another number (I. e.: placing a literal number
Immediately after a matched pattern), you cannot use the familiar \ 1
Notation for your backreference. \ 11, for example, wocould confuse
Preg_replace () since it does not know whether you want the \ 1
Backreference followed by a literal 1, or the \ 11 backreference
Followed by nothing. In this case the solution is to use $ {1} 1. This
Creates an isolated $1 backreference, leaving the 1 as a literal.

When using the deprecated e modifier, this function escapes some
Characters (namely ', ", and NULL) in the strings that replace

  1. This is done to ensure that no syntax errors arise

  2. Backreference usage with either single or double quotes (e.g.

'Strlen ('$ 1') + strlen ("$2")'). Make sure you are aware of PHP's
String syntax to know exactly how the interpreted string will look.

Related Article

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.