Reverse reference of Regular Expression in preg_replace

Source: Internet
Author: User

Recently, I encountered a problem: to retrieve the SRC of all IMG in a log, and then regenerate the SRC Based on the SRC content, and use the preg_replace () function in PHP (), the reverse reference of a regular expression is used:

 $ Content     =     '  Text text ; other text ;  '  ;

Function Img_convert ( $ Img , $ Param ){
$ Filepath = Getblogpicurl ( $ Img ); // Call getblogpicurl () to regenerate the new SRC value.
$ Param = Str_replace ( " \\ " , $ Param );
$ Str = ' IMG src = "http://www.codebean.cn/wp-admin/ ' . $ Filepath . ' "Alt =" "/> ' ;
}

Function Getblogpicurl ( $ Img ){
// Re-generate the SRC valueCode
}

$ Content = Preg_replace ( " // \/IE " , " Img_convert ('$ 2',' $1 $3 ') " , $ Content );

Here we use the reverse reference of the regular expression. when calling the img_convert () function, what is passed to img_convert () is the values of the regular expression matching three () respectively, they are in the format of $1, $2, $3, \ 1, \ 2, \ 3 respectively.

In addition, I directly use the img_convert () function in the replacement part of preg_replace, which requires adding the E parameter in the regular expression.

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.