Page 1 of 1
problem with str_replace()
Posted: Wed Jun 25, 2008 7:32 am
by avlisodraude
Hi,
I have been trying to use str_replace() for replacement on a string, most of the time it works but there are situations in which it does not.
Here is one example of what happens:
$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 $newphrase;
and 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 wrong with that function?
Re: problem with str_replace()
Posted: Wed Jun 25, 2008 8:45 am
by Jasheppard
ahh i see the problem!
results
and 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 )
the reason its saying ..0700797 is because it replaces it with .0797 but you also have it replacing 0 which there is a 0 in .0797 so it then becomes ..0700797
You will have to find a way around that? i cant think of a way?
very glitchy i say?
Re: problem with str_replace()
Posted: Wed Jun 25, 2008 8:54 am
by Jasheppard
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
-- sorry pickle, i will remember next time.
ahh oh i get it now...
i used
Code: Select all
<?php
$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);
$phrase = str_replace("10", "FLA", $phrase);
$phrase = str_replace("0", ".0700", $phrase);
$phrase = str_replace("4", ".0797", $phrase);
echo $phrase;
?>
and it works. becaus i made it replace 0 with .0700
BEFORE it replaces 4 with .0797
beacuse the old way was replaceing the string normally but then when it gets to replacing the 0, it replaces the 0 in the .0797 aswell.
Re: problem with str_replace()
Posted: Wed Jun 25, 2008 9:15 am
by avlisodraude
Thanks for the try Jasheppard,
but the problems is that the data is coming after reading a text file, so the numbers are unknown beforehand and therefore it is not possible for me to rearrange them for further processing.
Jasheppard wrote:ahh oh i get it now...
i used
Code: Select all
<?php
$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);
$phrase = str_replace("10", "FLA", $phrase);
$phrase = str_replace("0", ".0700", $phrase);
$phrase = str_replace("4", ".0797", $phrase);
echo $phrase;
?>
and it works. becaus i made it replace 0 with .0700
BEFORE it replaces 4 with .0797
beacuse the old way was replaceing the string normally but then when it gets to replacing the 0, it replaces the 0 in the .0797 aswell.
Re: problem with str_replace()
Posted: Thu Jun 26, 2008 2:17 am
by Jasheppard
i cant think right now... replacing numbers with other numbers, too glitchy.
Unless you covert the first lot of numbers into characters then after convert the characters back to numbers.
bit confusing?