How do I overwrite this function with Preg_replace_callback ()?

Source: Internet
Author: User
Recent projects, after updating PHP found Preg_replace () from version 5.5.0 /eModifier has been deprecated. And 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);

Ask the Great God for advice

Reply content:

Recent projects, after updating PHP, found that the preg_replace () modifier has been deprecated in the 5.5.0 version. /e And 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);

Ask the Great God for advice

preg_replace()The /e pattern was abandoned, not abandoned, preg_replace() okay! Please do not generalize the words!! I was scared to pee at first, and I thought PHP was so casual.

preg_replace_callback()is not very simple, is to replace with the function with the eval execution string ah. But honestly, I don't know how to do that. What exactly do you want to do with the replacement execution function, as if you want to re-write the line break according to 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);

The parameter of the function $matches is the result of your $search match, if you still do not understand or look at the manual, the manual has been written in full.

Here is the answer given by the official 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);

  • 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.