Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.albasani.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.php Subject: Re: Purpose for a str_replace with an embedded print_r return Date: Tue, 04 Nov 2014 16:58:46 +0100 Organization: PointedEars Software (PES) Lines: 62 Message-ID: <2023723.LoZblivUAB@PointedEars.de> References: <064143e2-28d1-40e4-a76e-c6d28b05dddb@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1415116743 29564 eJwNy8ERACEIA8CWDiTBdhBM/yWcs+/ForEzCAYExdzocFOfY9yqm8+EYHV9c7Aw9Sn1ljt/NzURxw== (4 Nov 2014 15:59:03 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 4 Nov 2014 15:59:03 +0000 (UTC) User-Agent: KNode/4.14.1 X-User-ID: eJwFwYEBwCAIA7CXcNAi5yiV/09YAudiZxAMDMYT9RB9ZZPnUpN9VxzofNZBWZW39o7l8x5Y8b0xGYXC6AdnphYB Cancel-Lock: sha1:W9rMO9wRt+wMsvPOKIIOZqDMkiE= X-NNTP-Posting-Host: eJwNyMEBwCAIA8CVpIaI4wSU/Uew9zyfNNYCnfD2Hsowjp0hU4O3kApau51PB7VD1/RfzoXoByb1EbU= Xref: csiph.com comp.lang.php:14462 Dave Patoch wrote: [restored context] > Christoph M. Becker wrote: >> Dave Patoch wrote: >>> 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) >> >> What type of value does $message hold? > > It's just a plain old string, which is why it confused me. I think the proper idiom is “to kill two birds with one stone”. This appears to be debug code, and if you pass “true” as second parameter to print_r(), it returns the output instead of printing it. The major advantage of print_r() over echo and print(), which always output the *string representation* of a value, is that the type of the first parameter does not matter for inspection: if it is a string or a number, the result is a string or number; if it is an array, it contains the array keys and values; and if it is a reference to an object, it contains the object's class name and properties. var_dump()'s output is more detailed than print_r()'s on the one hand, and trimmed at a point on the other, and highlighting can be applied to that with Xdebug; but it cannot return the output instead (you would have to output_buffer it yourself). str_replace() then normalizes the value so that it can be explode()d, for “foreach” iteration, without using regular expressions. This does not account for Mac OS < 10.x newlines ("\r"), but there does not appear to be a PHP release for those versions (anymore). You are _not_ posting to a Web forum or bulletin board here, but to a Usenet newsgroup where postings are read differently than in the Google Groups Web archive, and where they usually expire. Therefore, please provide attribution of quoted material, and a minimum of the context. -- PointedEars Zend Certified PHP Engineer Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.