In this code, replace the preg_replace function with the preg_replace_callback function. how can I write this function? Because the/e mode of preg_replace is not used in PHP5.5, we recommend that you replace the preg_replace_callback function. However, the problem arises. I don't know how to write the lower part of the code. this is the code for replacing the template class variables.
/* Indicates characters that affect the regular expression. for example, <{}> escape \\{\}\> */$ Left = preg_quote ($ this-> Delimiter_Left ); $ Right = preg_quote ($ this-> Delimiter_Right); $ pattern = array (/* 1. replace the variable */'in the template */'/'. $ Left. '\ s * \ $([A-Za-z0-9 _] *) \ s *'. $ Right. '/I',/* 2. match if */'in the template */'/'. $ Left. '\ s * if \ s *(. + ?) \ S * '. $ Right.' (. + ?) '. $ Left. '\ s * \/if \ s *'. $ Right. '/is'); $ replacement = array (/* 1. replace the common variable */'in the template */'
Tpl_Vars ["$ {1}"];?> ',/* 2. replace the if string in the template */'this is the IF field',);/* start to replace the tag */$ this-> Template_HTML = preg_replace ($ pattern, $ replacement, $ this-> Template_HTML); return $ this-> Template_HTML;
Reply to discussion (solution)
You do not use the e mode, so you do not need to rewrite it.
You do not use the e mode, so you do not need to rewrite it.
No. The/e mode is used in this code. you can see that the value stored in the array to be replaced is the PHP code, this code is correct in/e mode. as a result, PHP 5.5 and later versions will discard the/e mode. how can I get it wrong with preg_replace_callback, the key contains a reverse reference of $ {1}. an error is returned when writing the code.
$ Pattern = array (
/* 1. replace the variable in the template */
'/'. $ Left. '\ s * \ $([A-Za-z0-9 _] *) \ s *'. $ Right. '/I ',
/* 2. match if */in the template */
'/'. $ Left. '\ s * if \ s * (. + ?) \ S * '. $ Right.' (. + ?) '. $ Left.' \ s * \/if \ s * '. $ Right.'/is'
);
Which one uses the e mode?
You should give the original code, not the code you modified!
$ Pattern = array (
/* 1. replace the variable in the template */
'/'. $ Left. '\ s * \ $([A-Za-z0-9 _] *) \ s *'. $ Right. '/I ',
/* 2. match if */in the template */
'/'. $ Left. '\ s * if \ s * (. + ?) \ S * '. $ Right.' (. + ?) '. $ Left.' \ s * \/if \ s * '. $ Right.'/is'
);
Which one uses the e mode?
You should give the original code, not the code you modified!
/* Indicates characters that affect the regular expression. for example, <{}> escape \\{\}\> */$ Left = preg_quote ($ this-> Delimiter_Left ); $ Right = preg_quote ($ this-> Delimiter_Right); $ pattern = array (/* 1. replace the variable */'in the template */'/'. $ Left. '\ s * \ $([A-Za-z0-9 _] *) \ s *'. $ Right. '/I',/* 2. match if */'in the template */'/'. $ Left. '\ s * if \ s *(. + ?) \ S * '. $ Right.' (. + ?) '. $ Left. '\ s * \/if \ s *'. $ Right. '/ies'); $ replacement = array (/* 1. replace the common variable */'in the template */'
Tpl_Vars ["$ {1}"];?> ',/* 2. replace the if string in the template */'this is the IF field',);/* start to replace the tag */$ this-> Template_HTML = preg_replace ($ pattern, $ replacement, $ this-> Template_HTML); return $ this-> Template_HTML;
This is probably the eighth line. I have successfully replaced normal variables by preg_replace_callback ('Parameter 1', function ($ matches) {}, 'parameter 3. However, when the callback function of parameter 2 is written outside and used as a class method, the call is wrong, so I won't use it.
Public Function tpl_Replace () {/* indicates the characters that affect the regular expression. for example: <{}> escape \\{\}\> */$ Left = preg_quote ($ this-> Delimiter_Left); $ Right = preg_quote ($ this-> Delimiter_Right ); $ pattern = array (/* 1. replace the variable */'in the template */'/'. $ Left. '\ s * \ $([A-Za-z0-9 _] *) \ s *'. $ Right. '/I',/* 2. match if */'in the template */'/'. $ Left. '\ s * if \ s *(. + ?) \ S * '. $ Right.' (. + ?) '. $ Left. '\ s * \/if \ s *'. $ Right. '/is'); $ replacement = array (/* 1. replace the common variable */'in the template */'
Tpl_Vars ["$ {1}"];?> ',/* 2. replace the if string in the template */'this is the IF field',);/* start to replace the tag * // $ this-> Template_HTML = preg_replace ($ pattern, $ replacement, $ this-> Template_HTML); $ this-> Template_HTML = preg_replace_callback ($ pattern, function ($ matches) {/* 1. replace the common variable */$ replacement = $ this-> tpl_Vars [$ matches [1]; return $ replacement ;}, $ this-> Template_HTML) in the template ); return $ this-> Template_HTML ;}
The difference is that PHP is written in the function, and PHP code in the/e mode is written in the string. this is the essential difference. It seems a lot of trouble.
When I write the callback function out and use it as a class method, an error occurs during the call.
Public Function callback($matches){$replacement=$this->tpl_Vars[$matches[1]];return $replacement;}
$ Pattern = array (
/* 1. replace the variable in the template */
'/'. $ Left. '\ s * \ $([A-Za-z0-9 _] *) \ s *'. $ Right. '/I ',
/* 2. match if */in the template */
'/'. $ Left. '\ s * if \ s * (. + ?) \ S * '. $ Right.' (. + ?) '. $ Left.' \ s * \/if \ s * '. $ Right.'/ies'
);
Which one uses the e mode?
You should give the original code, not the code you modified!
/* Indicates characters that affect the regular expression. for example, <{}> escape \\{\}\> */$ Left = preg_quote ($ this-> Delimiter_Left ); $ Right = preg_quote ($ this-> Delimiter_Right); $ pattern = array (/* 1. replace the variable */'in the template */'/'. $ Left. '\ s * \ $([A-Za-z0-9 _] *) \ s *'. $ Right. '/I',/* 2. match if */'in the template */'/'. $ Left. '\ s * if \ s *(. + ?) \ S * '. $ Right.' (. + ?) '. $ Left. '\ s * \/if \ s *'. $ Right. '/is'); $ replacement = array (/* 1. replace the common variable */'in the template */'
Tpl_Vars ["$ {1}"];?> ',/* 2. replace the if string */' $ this-> stripvtags (\'
\ ', \' $ {2}
\ ')',);/* Start to replace the tag in the template file */$ this-> Template_HTML = preg_replace ($ pattern, $ replacement, $ this-> Template_HTML ); return $ this-> Template_HTML;
The previous code is wrong. This should be close to the original code. In fact, the original code is a piece of code in the simple template class in PHP. I want to copy a template class for my own program.
/* Indicates characters that affect the regular expression. for example, <{}> escape \\{\}\> */$ Left = preg_quote ($ this-> Delimiter_Left ); $ Right = preg_quote ($ this-> Delimiter_Right); $ pattern = array (/* 1. replace the variable */'in the template */'/'. $ Left. '\ s * \ $([A-Za-z0-9 _] *) \ s *'. $ Right. '/I',/* 2. match if */'in the template */'/'. $ Left. '\ s * if \ s *(. + ?) \ S * '. $ Right.' (. + ?) '. $ Left. '\ s * \/if \ s *'. $ Right. '/ies'); $ replacement = array (/* 1. replace the common variable */'in the template */'
Tpl_Vars ["$ {1}"];?> ',/* 2. replace the if string */' $ this-> stripvtags (\'
\ ', \' $ {2}
\ ')',);/* Start to replace the tag in the template file */$ this-> Template_HTML = preg_replace ($ pattern, $ replacement, $ this-> Template_HTML ); return $ this-> Template_HTML;
Corrected the problem. The Forum cannot be modified.