note 107411 added to function.addslashes

From: Date: Tue, 07 Feb 2012 10:42:35 +0000
Subject: note 107411 added to function.addslashes
Groups: php.notes 
Request: Send a blank email to php-notes+get-185890@lists.php.net to get a copy of this message
Is this function safe for UTF-8 ? Note that this function (addslashes()) gets called by preg_replace() function when using the "/e" modifier - so if you're using preg_replace() on UTF-8 data, but then preg_replace() is calling a function that's not UTF-8 safe (i.e., this function, addslashes() ) then all sorts of weirdness can happen... Here's an example: // goal: change the "[post=<> /]" present in $utf_data into a hyperlink: "<a href="site/post/id">Post Title</a>" $utf_data = 'some forum text in UTF8 format and a reference to a [post=4aa44b39dbeff00000b0 /]'; $html_ours[0] = '@\[p(?:ost){0,1}=(.{1,24})\ {0,}\/\]@Uieu'; $html_oursr[0] = 'debdcode_post( $1 )'; function debdcode_post( $in ) { // lookup the $in from database // return string return '<a href="/post/'. $in. '">Post Title</a>'; } $out = preg_replace( $html_ours, $html_oursr, $utf_data ); Whenever the string "=4a" (or any string of general pattern "=" then integer then at least one letter) is passed, preg_replace() bombs with an odd error, APPARENTLY because the addslashes() chokes on UTF-8. See bug report # 61001 Workaround appears to be using preg_replace_callback() instead of an array'd stack of pattern/replacements passed to preg_replace() ---- Server IP: 64.71.164.2 Probable Submitter: 70.91.82.193 ---- Manual Page -- http://www.php.net/manual/en/function.addslashes.php Edit -- https://master.php.net/note/edit/107411 Del: integrated -- https://master.php.net/note/delete/107411/integrated Del: useless -- https://master.php.net/note/delete/107411/useless Del: bad code -- https://master.php.net/note/delete/107411/bad+code Del: spam -- https://master.php.net/note/delete/107411/spam Del: non-english -- https://master.php.net/note/delete/107411/non-english Del: in docs -- https://master.php.net/note/delete/107411/in+docs Del: other reasons-- https://master.php.net/note/delete/107411 Reject -- https://master.php.net/note/reject/107411 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185890) next »