note 103555 deleted from function.str-replace by danbrown

From: Date: Wed, 20 Apr 2011 14:30:06 +0000
Subject: note 103555 deleted from function.str-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-178997@lists.php.net to get a copy of this message
Note Submitter: Dean ---- When using the following code from the example // Order of replacement $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; $order = array("\r\n", "\n", "\r"); $replace = '<br />'; // Processes \r\n's first so they aren't converted twice. $newstr = str_replace($order, $replace, $str); The \r\n won't get replaced. To get it to work change all occurrences of double quotes into single quotes so the code reads as follows. // Order of replacement $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; $order = array('\r\n', '\n', '\r'); $replace = '<br />'; // Processes \r\n's first so they aren't converted twice. $newstr = str_replace($order, $replace, $str); The code will then as expected. I just spent 20 minutes trying to get this code to work just to realise the quotes in the array are wrong.

« previous php.notes (#178997) next »