Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14476

Re: Purpose for a str_replace with an embedded print_r return

From Matthew Carter <m@ahungry.com>
Newsgroups comp.lang.php
Subject Re: Purpose for a str_replace with an embedded print_r return
Date 2014-11-05 15:51 -0500
Organization Ahungry (http://ahungry.com)
Message-ID <8761etz7yv.fsf@ahungry.com> (permalink)
References <bad4f2c1-102e-4115-94ea-c8f8efdaca0b@googlegroups.com> <m3deem$dhn$1@dont-email.me> <m3dgqc$vk3$1@dont-email.me>

Show all headers | View raw


Jerry Stuckle <jstucklex@attglobal.net> 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
>> <?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",
>> <<<EOT
>> 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

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Purpose for a str_replace with an embedded print_r return Dave Patoch <earthlydilemma@gmail.com> - 2014-11-04 06:20 -0800
  Re: Purpose for a str_replace with an embedded print_r return "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-04 16:08 +0100
    Re: Purpose for a str_replace with an embedded print_r return Dave Patoch <earthlydilemma@gmail.com> - 2014-11-04 07:27 -0800
      Re: Purpose for a str_replace with an embedded print_r return Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-04 16:58 +0100
        Re: Purpose for a str_replace with an embedded print_r return "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-04 18:58 +0100
          Re: Purpose for a str_replace with an embedded print_r return Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-04 13:54 -0500
            Re: Purpose for a str_replace with an embedded print_r return Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-05 14:27 +0100
              Re: Purpose for a str_replace with an embedded print_r return Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-05 08:46 -0500
                Re: Purpose for a str_replace with an embedded print_r return Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-05 15:32 +0100
                Re: Purpose for a str_replace with an embedded print_r return Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-05 10:44 -0500
                Re: Purpose for a str_replace with an embedded print_r return Matthew Carter <m@ahungry.com> - 2014-11-05 15:50 -0500
                Re: Purpose for a str_replace with an embedded print_r return Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-05 16:59 -0500
                Re: Purpose for a str_replace with an embedded print_r return "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-05 23:56 +0100
                Re: Purpose for a str_replace with an embedded print_r return Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-06 04:30 +0100
                Re: Purpose for a str_replace with an embedded print_r return Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-05 22:53 -0500
  Re: Purpose for a str_replace with an embedded print_r return Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-05 15:07 +0000
    Re: Purpose for a str_replace with an embedded print_r return Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-05 10:47 -0500
      Re: Purpose for a str_replace with an embedded print_r return Matthew Carter <m@ahungry.com> - 2014-11-05 15:51 -0500
        Re: Purpose for a str_replace with an embedded print_r return Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-05 17:00 -0500
          Re: Purpose for a str_replace with an embedded print_r return Matthew Carter <m@ahungry.com> - 2014-11-05 22:35 -0500
            Re: Purpose for a str_replace with an embedded print_r return Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-05 22:55 -0500
            Re: Purpose for a str_replace with an embedded print_r return Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-06 04:26 +0000
              Re: Purpose for a str_replace with an embedded print_r return Matthew Carter <m@ahungry.com> - 2014-11-06 00:09 -0500
                Re: Purpose for a str_replace with an embedded print_r return Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-06 23:59 +0000
            Re: Purpose for a str_replace with an embedded print_r return "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-06 18:43 +0100
        Re: Purpose for a str_replace with an embedded print_r return Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-06 02:39 +0000
      Re: Purpose for a str_replace with an embedded print_r return Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-06 02:44 +0000
        Re: Purpose for a str_replace with an embedded print_r return Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-05 22:56 -0500

csiph-web