The problem of preg_replace rewriting preg_replace_callback

Source: Internet
Author: User
The original code for preg_replace to rewrite preg_replace_callback is as follows:
$ Pattern = array ('/
 /', // Replace the smarty comment '/
 /', // Replace the html comment without line breaks'/(href = ["| \ ']) \. \. \/(. *?) (["| \ '])/I', // replace the relative link '/((? : Background | src) \ s * = \ s * ["| \ ']) (? : \. \/| \.\.\/)? (Images \/.*? ["| \ '])/Is', // add $ tmp_dir '/((? : Background | background-image): \ s *? Url \()(? : \. \/| \.\.\/)? (Images \/)/is ', // add $ tmp_dir'/{nocache} (. +?) to images ?) {\/Nocache}/ise ', // no Cache module); $ replace = array (' \ 1', '', '\ 1 \ 2 \ 3 ', '\ 1 '. $ tmp_dir. '\ 2',' \ 1 '. $ tmp_dir. '\ 2', "' {insert name = \" nocache \"'. '". $ this-> _ echash. "'. base64_encode ('\ 1 '). '}' ",); return preg_replace ($ pattern, $ replace, $ source );

The original server is php5.2. after being upgraded to php5.5, a program error is found.


Reply to discussion (solution)

$ Pattern = array ('/
 /', // Replace the smarty comment '/
 /', // Replace the html comment without line breaks'/(href = ["| \ ']) \. \. \/(. *?) (["| \ '])/I', // replace the relative link '/((? : Background | src) \ s * = \ s * ["| \ ']) (? : \. \/| \.\.\/)? (Images \/.*? ["| \ '])/Is', // add $ tmp_dir '/((? : Background | background-image): \ s *? Url \()(? : \. \/| \.\.\/)? (Images \/)/is ', // add $ tmp_dir before images); $ replace = array (' \ 1 ','', '\ 1 \ 2 \ 3',' \ 1 '. $ tmp_dir. '\ 2',' \ 1 '. $ tmp_dir. '\ 2',); $ source = preg_replace ($ pattern, $ replace, $ source); return preg_replace_callback ('/{nocache }(. + ?) {\/Nocache}/is ', function ($ m) {return' {insert name = "nocache "'. $ this-> echash. base64_encode ($ m [1]). '}';}, $ source );

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.