« Previous Next »

Thread: problem with str_replace

Last post 06-25-2008 6:31 PM by Alex.W474. 1 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (2 items)

Sort Posts:

  • 06-25-2008, 9:33 AM

    problem with str_replace

    I have a problem with str_replace():

    when I try to use it in this way:

    $phrase  = "INSERT INTO addressbook(user_id, email, name) VALUES( 10 , 4 , 0 ) ON DUPLICATE KEY UPDATE(user_id= 10 )";
    $healthy = array("10","4","0");
    $yummy   = array("FLA",".0797",".0700");

    $newphrase = str_replace($healthy, $yummy, $phrase);
    echo 'www: '.$newphrase;

    Instead of getting what I would expected:

    INSERT INTO addressbook(user_id, email, name) VALUES( FLA , .0797 , .0700 ) ON DUPLICATE KEY UPDATE(user_id= FLA )

    I get this:

    INSERT INTO addressbook(user_id, email, name) VALUES( FLA , ..0700797 , .0700 ) ON DUPLICATE KEY UPDATE(user_id= FLA )

    could someone please tell me what is the mistake or error?

    thanks in advance

  • 06-25-2008, 6:31 PM In reply to

    Re: problem with str_replace

    str_replace with array is identical to multiple str_replaces. So you replace 4 with .0797 and then replace 0 with .0700 but zeroes in the .0797 sequences are also replaced.

    Change the order of replaces:

    $healthy = array("10","0", "4");
    $yummy   = array("FLA",".0700",
    ".0797");

Page 1 of 1 (2 items)
Microsoft Communities