preg replace callback - what does this preg_replace_callback do in PHP? and how do I stop it leaking memory? -
i've got section of code on b2evo php site following:
$content = preg_replace_callback( '/[\x80-\xff]/', create_function( '$j', 'return "&#".ord($j[0]).";";' ), $content);
what section of code do? guess strips out ascii characters between 128 , 256, can't sure.
also, stands, every time bit of code called within page, php allocates , not free upto 2k of memory. if function called 1000+ times on page (this can happen), page uses 2mb of memory.
this causing problems web application. why losing memory, , how rewrite don't memory leak?
not stripping, replaces high-ascii characters entities.
see preg_replace_callback.
create_function used make anonymous function, can use plain function instead:
$content = 'Çà ! nœm dé fîçhïèr tôrdù, @ pöür têstër... ? ~ Œ[€]'; $content = preg_replace_callback('/[\x80-\xff]/', 'cb_chartoentity', $content); echo $econtent . '<br>'; echo htmlspecialchars($content) . '<br>'; echo htmlentities($content) . '<br>'; echo htmlentities($content, ent_noquotes, 'cp1252') . '<br>'; function cb_chartoentity($matches) { return '&#' . ord($matches[0]) . ';'; }
[edit] found cleaner, faster way job! ^_^ use htmlentities options fitting needs.
Comments
Post a Comment