Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Matthew Carter Newsgroups: comp.lang.php Subject: Re: Purpose for a str_replace with an embedded print_r return Date: Wed, 05 Nov 2014 15:51:52 -0500 Organization: Ahungry (http://ahungry.com) Lines: 72 Message-ID: <8761etz7yv.fsf@ahungry.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="7c986cd4736462de309a749b207746fe"; logging-data="15993"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Kz/Jk3FolmpCzisnsZtrK" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:kU7FsSLdwxMEg1o4zRlwL7ngD54= sha1:/pryLxh4XC1lNZ/j1zd7BLPrzzc= Xref: csiph.com comp.lang.php:14476 Jerry Stuckle writes: > On 11/5/2014 10:07 AM, Denis McMahon wrote: >> On Tue, 04 Nov 2014 06:20:24 -0800, Dave Patoch wrote: >> >>> I'm cleaning up a legacy code base and I've been trying to figure out >>> what the functional difference (other then a few cpu ticks and a bit of >>> memory) is between: >>> >>> foreach( >>> explode("\n", str_replace("\r\n", "\n", print_r($message, true))) as >>> $msg) >>> >>> and >>> >>> foreach( >>> explode("\n", str_replace("\r\n", "\n", $message)) as $msg) >> >> I've not managed to find an edge case that is handled differently by the >> two cases, tried feeding a mixture of single and multi line strings into >> the explodes and used !== to check if the resulting arrays were different. >> >> #!/usr/bin/php >> > >> $test = array( >> "This is a single line string", >> "This is a multi line string\nThis is line 2 of the string\nAnd here >> is line 3", >> "This is another multi line string\r\nThis is line 2 of the string\r >> \nAnd here is line 3", >> "This is a multi line string\nWith a blank line\n\nAnd here is line >> 4", >> "This is another multi line string\r\nWith a blank line\r\n\r\nAnd >> here is line 4", >> <<> Blah Blah Blah >> Blah bloody blah >> Pointy eared blah >> EOT >> ); >> >> foreach ($test as $t) { >> $r1 = explode("\n", str_replace("\r\n", "\n", print_r($t, true))); >> $r2 = explode("\n", str_replace("\r\n", "\n", $t)); >> if ($r1 !== $r2) echo "\n\n{$t}\n\n differs\n"; >> } >> >> echo "Tests complete\n"; >> >> ?> >> > > Denis, > > You won't. The code generates the same result in both cases because > you're dealing with string. > > Now if it were a complex type such as an array or object, the results > would differ. But the OP already indicated it's a string, so the extra > call to print_r() is superfluous (and confusing). I added to the other thread, but try your tests with: chr(980); Under the UTF-8 character set (assuming this is coming from a browser/over mod_php). -- Matthew Carter (m@ahungry.com) http://ahungry.com