Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

php5.5 deprecated preg_replace.
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Installation and Setup
View previous topic :: View next topic  
Author Message
praotes
Smarty n00b


Joined: 15 Jul 2013
Posts: 3

PostPosted: Mon Jul 15, 2013 1:31 am    Post subject: php5.5 deprecated preg_replace. Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Jul 15, 2013 6:36 pm    Post subject: Reply with quote

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
View user's profile Send private message
praotes
Smarty n00b


Joined: 15 Jul 2013
Posts: 3

PostPosted: Tue Jul 16, 2013 3:01 pm    Post subject: Disabling e_deprecated Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Jul 16, 2013 7:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
praotes
Smarty n00b


Joined: 15 Jul 2013
Posts: 3

PostPosted: Tue Jul 16, 2013 9:14 pm    Post subject: Superb... Reply with quote

Thanks so very much!! Very much appreciated.

Have a great day in Hamburg Smile

Greetings from BC, Canada,

Hans
Back to top
View user's profile Send private message
leandromagela
Smarty n00b


Joined: 12 Aug 2014
Posts: 1

PostPosted: Tue Aug 12, 2014 7:09 pm    Post subject: function preg_replace_callback Reply with quote

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
View user's profile Send private message
slr772s
Smarty n00b


Joined: 04 Apr 2015
Posts: 3

PostPosted: Sat Apr 04, 2015 10:44 am    Post subject: Re: function preg_replace_callback Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Apr 04, 2015 6:01 pm    Post subject: Reply with quote

I don't see any issues with your code, nor do I see, how it is related to Smarty. Please elaborate.
Back to top
View user's profile Send private message
slr772s
Smarty n00b


Joined: 04 Apr 2015
Posts: 3

PostPosted: Sun Apr 05, 2015 4:20 am    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Apr 05, 2015 4:34 am    Post subject: Reply with quote

Just update your Smarty version.
Back to top
View user's profile Send private message
slr772s
Smarty n00b


Joined: 04 Apr 2015
Posts: 3

PostPosted: Sun Apr 05, 2015 4:55 am    Post subject: Reply with quote

AnrDaemon wrote:
Just update your Smarty version.


Sorry Confused Confused Confused I don't understand What should I do!!! Sad Sad Sad Sad
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Apr 05, 2015 11:52 am    Post subject: Reply with quote

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
View user's profile Send private message
Seomurai
Smarty n00b


Joined: 10 Jan 2019
Posts: 2

PostPosted: Thu Jan 10, 2019 10:46 am    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jan 10, 2019 1:32 pm    Post subject: Reply with quote

Just update your Smarty2. The fix is implemented since years.
Back to top
View user's profile Send private message
Seomurai
Smarty n00b


Joined: 10 Jan 2019
Posts: 2

PostPosted: Thu Jan 10, 2019 1:39 pm    Post subject: Reply with quote

Do you really think I would take the time to fix it manually if I could update it..?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Installation and Setup All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP