[ale] php weirdness

Jim Kinney jim.kinney at gmail.com
Mon Feb 23 13:50:44 EST 2009


I'm bumfuzzled.

Originally, my output was abcabc-12345. What I wanted was abc12345->
abc-12345 (inserting a - between chars 3 & 4).

echo str_replace(substr($row['foo'],0,3), substr($row['foo'],0,3).'-',
$row['foo']);
abc-12345

is doing what it's supposed to do now.

I ran the original version 8-10 times with same results (restarted web
server, refreshed screen, added extra printf's , etc and each time I
got a doubled abcabc. Now it works.

Sometimes computers are just weird.

On Mon, Feb 23, 2009 at 12:50 PM, James Sumners <james.sumners at gmail.com> wrote:
> The following does exactly what you want:
> ---------------------------------
> <?php
> $foo = "abc12345";
> $foo2 = substr_replace($foo, '-', 0, 3);
> $foo3 = str_replace(substr($foo, 0, 3), '-', $foo);
> print("foo = ".$foo."\n");
> print("foo2 = ".$foo2."\n");
> print("foo3 = ".$foo3."\n");
> ?>
> ----------------------------------
>
> This is the output from my installation of PHP:
> ----------------------------------
> [mythtv at mythtv tmp]$ php -v && php test.php
> PHP 5.2.7 with Suhosin-Patch 0.9.6.3 (cli) (built: Dec  6 2008 19:19:58)
> Copyright (c) 1997-2008 The PHP Group
> Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
> foo = abc123456
> foo2 = -123456
> foo3 = -123456
> ----------------------------------
>
> We have done nothing different according to what you have posted. So
> there must be something else modifying your string.
>
> On Mon, Feb 23, 2009 at 12:24 PM, Jim Kinney <jim.kinney at gmail.com> wrote:
>> Why does
>>
>> echo str_replace(substr($row['foo'],0,3), '-', $row['foo']);
>>
>> print out    abc-12345
>>
>> when $row['foo'] contains abc12345? From what str_replace is SUPPOSED
>> to do it should print -12345 instead??
>>
>> php5.
>
>
>
> --
> James Sumners
> http://james.roomfullofmirrors.com/
>
> "All governments suffer a recurring problem: Power attracts
> pathological personalities. It is not that power corrupts but that it
> is magnetic to the corruptible. Such people have a tendency to become
> drunk on violence, a condition to which they are quickly addicted."
>
> Missionaria Protectiva, Text QIV (decto)
> CH:D 59
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
>



-- 
-- 
James P. Kinney III


More information about the Ale mailing list