note 113373 added to function.htmlspecialchars

From: Date: Thu, 03 Oct 2013 08:31:22 +0000
Subject: note 113373 added to function.htmlspecialchars
Groups: php.notes 
Request: Send a blank email to php-notes+get-196453@lists.php.net to get a copy of this message
For those having problems after the change of default value of $encoding argument to UTF-8 since PHP 5.4. If your old non-UTF8 projects ruined - pls consider: 1. http://php.net/manual/en/function.override-function.php 2. http://php.net/manual/ru/function.runkit-function-redefine.php The idea - you override the built-in htmlspecialchars() function with your customized variant which is able to respect non UTF-8 default encoding. This small piece of code can be then easily inserted somewhere at the start of yout project. No need to rewrite all htmlspecialchars() entries globally. I've spent several hours with both approaches. Variant 1 looks good especaially in combination with http://www.php.net/manual/en/function.rename-function.php as it allows to call original htmlspecialchars() with just altered default args. The code could be as follows: <?php rename_function('htmlspecialchars', 'renamed_htmlspecialchars'); function overriden_htmlspecialchars($string, $flags=NULL, $encoding='cp1251', $double_encode=true) { $flags = $flags ? $flags : (ENT_COMPAT|ENT_HTML401); return renamed_htmlspecialchars($string, $flags, $encoding, $double_encode); } override_function('htmlspecialchars', '$string, $flags, $encoding, $double_encode', 'return overriden_htmlspecialchars($string, $flags, $encoding, $double_encode);'); ?> Unfortunatelly this didn't work for me properly - my site managed to call overriden function but not every time I reloaded the pages. Moreover other PHP sites crashed under my Apache server as they suddenly started blaming htmlspecialchars() was not defined. I suppose I had to spend more time to make it work thread/request/site/whatever-safe. So I switched to runkit (variant 2). It worked for me, although even after trying runkit_function_rename()+runkit_function_add() I didn't managed to recall original htmlspecialchars() function. So as a quick solution I decided to call htmlentities() instead: <?php function overriden_htmlspecialchars($string, $flags=NULL, $encoding='UTF-8', $double_encode=true) { $flags = $flags ? $flags : (ENT_COMPAT|ENT_HTML401); $encoding = $encoding ? $encoding : 'cp1251'; return htmlentities($string, $flags, $encoding, $double_encode); } runkit_function_redefine('htmlspecialchars', '$string, $flags, $encoding, $double_encode', 'return overriden_htmlspecialchars($string, $flags, $encoding, $double_encode);'); ?> You may be able to implement your more powerfull overriden function. Good luck! ---- Server IP: 69.147.83.199 Probable Submitter: 212.113.124.110 ---- Manual Page -- http://php.net/manual/en/function.htmlspecialchars.php Edit -- https://master.php.net/note/edit/113373 Del: integrated -- https://master.php.net/note/delete/113373/integrated Del: useless -- https://master.php.net/note/delete/113373/useless Del: bad code -- https://master.php.net/note/delete/113373/bad+code Del: spam -- https://master.php.net/note/delete/113373/spam Del: non-english -- https://master.php.net/note/delete/113373/non-english Del: in docs -- https://master.php.net/note/delete/113373/in+docs Del: other reasons-- https://master.php.net/note/delete/113373 Reject -- https://master.php.net/note/reject/113373 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196453) next »