note 34981 deleted from function.str-replace by didou

From: Date: Sat, 16 Aug 2003 09:51:06 +0000
Subject: note 34981 deleted from function.str-replace by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-54312@lists.php.net to get a copy of this message
Note Submitter: number_co@yahoo.com ---- 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.

« previous php.notes (#54312) next »