Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

php5.5 deprecated preg_replace.

 
Post new topic   Reply to topic    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: 4920
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: 4920
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
Smarty Elite


Joined: 03 Dec 2012
Posts: 341

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
Smarty Elite


Joined: 03 Dec 2012
Posts: 341

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: 4920
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
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Installation and Setup All times are GMT
Page 1 of 1

 
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