Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the error of PHP7 preg_replace

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve PHP7 preg_replace errors". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to solve PHP7 preg_replace errors.

Problem description:

PHP7 abandoned preg_replace?

It was originally cleared in php5 to handle the later parameter substitution in url, as follows

$url = preg_replace ('/ ([? &]) src= [^ &] + (&?) / eBay,'"$2" = = "?": "$1", $url)

But the error was reported in php7.

I need to replace it with preg_replace_callback. What should I do?

Related code

$url = preg_replace ('/ ([? &]) src= [^ &] + (&?) / eBay,'"$2" = = "?": "$1", $url); problem analysis:

The e modifier has been marked as content to be removed since 5.3 because of security concerns.

Instead, it is replaced by preg_replace_callback. The second parameter of this method is a callback function, which automatically passes in the matching packet as the parameter. The matching group is accessed through the array subscript inside the callback function. (the mobile phone codeword is not formatted)

Preg_replace_callback ('/ ([? &]) src= [^ &] + (&?) /', function ($matches) {return $matches [2] = = "?": $matches [1];}, $url)

Knowledge point expansion:

PHP7 has removed the e modifier of preg_replace

The official website prompts that support for the / e modifier has been removed. Please use preg_replace_callback () instead

The reason is that the / e modifier causes preg_replace () to treat the replacement parameter as PHP code (after the appropriate reverse reference has been replaced), which will be used by the backdoor of a sentence.

Look at smarty is also used in this way, there is also a problem

$source_content = preg_replace ($search.'e', "'". $this- > _ quote_replace ($this- > left_delimiter). 'php'. "'. Str_repeat (\"\ n\ ", substr_count ('\\ 0mm,\"\ n\ "). $this- > _ quote_replace ($this- > right_delimiter). "'", $source_content); you can modify the smarty template to this $source_content = preg_replace_callback ($search, function ($matches) {$str= ""; $str.=$this- > _ quote_replace ($this- > left_delimiter). 'php';$str.=str_repeat ("\\ n\\", substr_count ($matches [1], "\\ n\\")); $str.=$this- > _ quote_replace ($this- > right_delimiter); return $str;}, $source_content) Thank you for your reading, the above is the content of "how to solve PHP7 preg_replace errors". After the study of this article, I believe you have a deeper understanding of how to solve the problem of PHP7 preg_replace errors, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 236

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report