note 34981 added to function.str-replace

From: Date: Wed, 13 Aug 2003 23:47:44 +0000
Subject: note 34981 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-54117@lists.php.net to get a copy of this message
nospam at floconsdepaques dot com dot com wrote this: "You need to put line (1) after line (3) like this : str_repalce("\'", "\\\'", $str) (a) str_repalce("\\", "\\\\", $str) (b) str_repalce("\"", "\\\"", $str) (c) In this case, when " is encoutered, line (c) will return a correct escaped \" ." That is more correct than nimrod leahvi's answer, but it is still wrong. For instance, if an apostrophe (') was encountered, line 1 will return \', but line 2 will return \\'. You need to put the backslash-replacement line first before any other add-backslash-to-special-character as follows: str_repalce("\\", "\\\\", $str) (1) // <--- this line must be first str_repalce("\'", "\\\'", $str) (2) str_repalce("\"", "\\\"", $str) (3) In this case, when " and ' are encountered, lines 2 and 3 will return \' and \", respectively. ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34981 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34981&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34981&report=yes

« previous php.notes (#54117) next »