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.