View previous topic :: View next topic |
Author |
Message |
praotes Smarty n00b
Joined: 15 Jul 2013 Posts: 3
|
Posted: Mon Jul 15, 2013 1:31 am Post subject: php5.5 deprecated preg_replace. |
|
|
Dear Smarty Colleagues:
We have upgraded our server to php5.5 and experience the following:
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 |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Jul 15, 2013 6:36 pm Post subject: |
|
|
The /e modifier is just deprecated in php 5.5.
If you exclude E_DEPRECATED from error reporting the old code should still work. |
|
Back to top |
|
praotes Smarty n00b
Joined: 15 Jul 2013 Posts: 3
|
Posted: Tue Jul 16, 2013 3:01 pm Post subject: Disabling e_deprecated |
|
|
Dear U-Tews:
I can follow that logic, and it is good to hear, but we rather only exclude STRICT error reporting, and like to see everything else so we can act on errors in a forward compatibility sort of way.
Would you be able to recommend how we adjust the Smarty 2.x code line? We have no option to upgrade to 3.x as it requires too many corrections all across our large application.
Hope you can assist. awaiting your thoughts, with warm regards,
Hans |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jul 16, 2013 7:49 pm Post subject: |
|
|
Here is the fix
Code: | $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);
|
It'S also in the SVN at
https://smarty-php.googlecode.com/svn/branches/Smarty2Dev |
|
Back to top |
|
praotes Smarty n00b
Joined: 15 Jul 2013 Posts: 3
|
Posted: Tue Jul 16, 2013 9:14 pm Post subject: Superb... |
|
|
Thanks so very much!! Very much appreciated.
Have a great day in Hamburg
Greetings from BC, Canada,
Hans |
|
Back to top |
|
leandromagela Smarty n00b
Joined: 12 Aug 2014 Posts: 1
|
Posted: Tue Aug 12, 2014 7:09 pm Post subject: function preg_replace_callback |
|
|
Before:
$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);
After:
$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);
Success! |
|
Back to top |
|
slr772s Smarty n00b
Joined: 04 Apr 2015 Posts: 3
|
Posted: Sat Apr 04, 2015 10:44 am Post subject: Re: function preg_replace_callback |
|
|
Hi Every Body
Sorry I don't know PHP very much. I have the problem as yours, so if it is possible please solve my problem.
The source code is below and it's appreciate if you change it to the correct type to help me don't get the error anymore.
------------------------------------------------------------------------------
// Run our defined search-and-replace
$text = preg_replace($this->search, $this->replace, $text);
// Strip unknown characters
$text = preg_replace('/&[^&;]+;/i', '', html_entity_decode($text, ENT_COMPAT, 'UTF-8'));
// Strip any other HTML tags
$text = strip_tags($text, $this->allowed_tags);
// Bring down number of empty lines to 2 max
$text = preg_replace("/\n\s+\n/", "\n\n", $text);
$text = preg_replace("/[\n]{3,}/", "\n\n", $text);
------------------------------------------------------------------------------ |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Apr 04, 2015 6:01 pm Post subject: |
|
|
I don't see any issues with your code, nor do I see, how it is related to Smarty. Please elaborate. |
|
Back to top |
|
slr772s Smarty n00b
Joined: 04 Apr 2015 Posts: 3
|
Posted: Sun Apr 05, 2015 4:20 am Post subject: |
|
|
AnrDaemon wrote: | I don't see any issues with your code, nor do I see, how it is related to Smarty. Please elaborate. |
Thanks for attention.
When I browse my web, some times I get this error
[8192]: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead (HTML2Text/class.html2text.php:438)
The source code that I copied in the last post begin with the line 438 in the class.html2text.php file. So I think the problem should be the deprecated preg_replace. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Apr 05, 2015 4:34 am Post subject: |
|
|
Just update your Smarty version. |
|
Back to top |
|
slr772s Smarty n00b
Joined: 04 Apr 2015 Posts: 3
|
Posted: Sun Apr 05, 2015 4:55 am Post subject: |
|
|
AnrDaemon wrote: | Just update your Smarty version. |
Sorry I don't understand What should I do!!! |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Apr 05, 2015 11:52 am Post subject: |
|
|
AnrDaemon wrote: | Just update your Smarty version. |
This will not solve the problem as (HTML2Text/class.html2text.php iss not a class of the Smarty package.
You should contact the people who provided your software. |
|
Back to top |
|
Seomurai Smarty n00b
Joined: 10 Jan 2019 Posts: 2
|
Posted: Thu Jan 10, 2019 10:46 am Post subject: |
|
|
Hello everyone!
Sorry to post in a so old topic but I'm updating my website that uses Smarty 2 for PHP 7 and I have a question concerning this part of the code.
I did replace it by the following fix based on the one that U.Tews posted and it works:
Code: |
$source_content = preg_replace_callback($search, 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);
|
But I don't understand what the original code is supposed to do exactly and especially the part "substr_count('\\0', \"\n\")", what is this \\0?
I would like to understand it well to be sure that my fix is correct. My concern is also about the "matches[1]", why not "matches[0]"?
Thank you in advance for your replies.
Seomurai |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Jan 10, 2019 1:32 pm Post subject: |
|
|
Just update your Smarty2. The fix is implemented since years. |
|
Back to top |
|
Seomurai Smarty n00b
Joined: 10 Jan 2019 Posts: 2
|
Posted: Thu Jan 10, 2019 1:39 pm Post subject: |
|
|
Do you really think I would take the time to fix it manually if I could update it..? |
|
Back to top |
|
|