Jump to content

php5.5 preg_replace(): The /e modifier is deprecated, use preg_replace_callback


Recommended Posts

Dear Colleagues: 

 

We have upgraded our server to php5.5 and experience the following, as we run the Smarty template: 

 

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in xxx/xxxx/php5/smarty/base/Smarty_Compiler.class.php on line 270 

 

The code as it currently is reads this... 

 

/* replace special blocks by "{php}" */ 

$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); 

 

QUESTION: Can you please explain what I need to change. The code directly below is identical to the one above, EXCEPT I replaced preg_replace with preg_replace_callback. The php5.5 instructions say I need to replace the "replacement" param with specifying a "callback." I know too little of php to do this, nor do I find a clear cut "replacement" param in this code. 

 

$source_content = preg_replace_callback($search.'e', "'" 

. $this->_quote_replace($this->left_delimiter) . 'php' 

. "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'" 

. $this->_quote_replace($this->right_delimiter) 

. "'" 

, $source_content);

 

Hope you can assist. Thanks! 

 

Hans

Upgrading to 5.5? Jumping the gun a bit much?

 

preg_replace('/expr/e', 'foo', $string)
is basically equivalent to

preg_replace_callback('/expr/', function($matches) {
    return foo;
}, $string)
where foo is actual PHP code. You also have to deal with closures and scope, though, so your code ends up looking something like

$self = $this;
$source_content = preg_replace_callback($search, function($matches) use ($self) {
	return $self->_quote_replace($self->left_delimiter)
	. "php"
	. str_repeat("\n", substr_count($matches[0], "\n")
	. $self->_quote_replace($self->right_delimiter)
}, $source_content);

Dear Requinix:

 

Apologies for the late reply. I was called away yesterday.

 

Thanks for this coaching. We're merely working towards the future. PHP5.5 is supposed to be "stable" and PCI compliance seems to demand it. 

Thanks again,

 

Hans

PHP5.5 is supposed to be "stable" and PCI compliance seems to demand it.

"Supposed". That's the point. I applaud you moving to 5.5, don't get me wrong there. I just find it surprising someone would move to a .0 release of PHP that's not even a month old.

 

PCI compliance probably wants you to be up to date with security patches. There's no way it could/should require you to be on the latest and greatest version of a product.

  • 3 months later...

"Supposed". That's the point. I applaud you moving to 5.5, don't get me wrong there. I just find it surprising someone would move to a .0 release of PHP that's not even a month old.

 

PCI compliance probably wants you to be up to date with security patches. There's no way it could/should require you to be on the latest and greatest version of a product.

 

 you can convey a point to someone more effectively by being a bit more down to earth. also i'm on php 5.5 as well, and still using preg with no problems. oddly enough, my ajax scripts seem to break with every upgrade, and the move to 5.5 was no exception.

Edited by r3wt

That's the point. I applaud you moving to 5.5, don't get me wrong there. I just find it surprising someone would move to a .0 release of PHP that's not even a month old.

5.5.0 was released in June, 5.5 is certainly stable and is currently at 5.5.5

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.