How to Use preg_replace_callback () to rewrite this function?

Source: Internet
Author: User
In the latest project, after updating PHP, we found that the e modifier of preg_replace () has been discarded since version 5.5.0. I don't know how to rewrite {code...}. Could you advise me on the latest project? After updating PHP, I found that preg_replace () started in version 5.5.0. /eThe modifier has been discarded. Then I don't know how to rewrite it.

$source_content = preg_replace($search.'e', "'"                                       . $this->_quote_replace($this->left_delimiter) . 'php'                                       . "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"                                       . $this->_quote_replace($this->right_delimiter)                                       . "'"                                       , $source_content);

Please advise

Reply content:

In the latest project, after updating PHP, we found that preg_replace () started in version 5.5.0./eThe modifier has been discarded. Then I don't know how to rewrite it.

$source_content = preg_replace($search.'e', "'"                                       . $this->_quote_replace($this->left_delimiter) . 'php'                                       . "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"                                       . $this->_quote_replace($this->right_delimiter)                                       . "'"                                       , $source_content);

Please advise

Yespreg_replace()Of/eThe mode has been deprecated, notpreg_replace()Abandoned !! Please do not speak in an abstract way !! I was so scared at the beginning that I thought PHP was so casual.

preg_replace_callback()Isn't it very easy? I just replaced it with a function.evalExecution string. But I honestly don't know what your replacement execution function is doing. It seems that you want to rewrite the line feed based on the number of line breaks ?? Write a reference for you:

$source_content = preg_replace_callback($search, function($matches) {    return str_repeat("\n", substr_count($matches[0], "\n");}, $cource_content);

Function$matchesThe parameter is yours.$searchIf you still don't understand the matching result, check the manual. The manual has been fully written.

Here is the answer officially provided by smarty:
$ Source_content = preg_replace_callback ($ search, create_function ('$ matches', "return '"
. $ This-> _ quote_replace ($ this-> left_delimiter). 'php'
. "'. Str_repeat (\" \ n \ ", substr_count (' \ $ matches [1] ', \" \ n \")).'"
. $ This-> _ quote_replace ($ this-> right_delimiter)
. "';"), $ Source_content );

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.