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?
problem with str_replace()
Moderator: General Moderators
- Jasheppard
- Forum Newbie
- Posts: 24
- Joined: Tue Jun 17, 2008 11:44 pm
Re: problem with str_replace()
ahh i see the problem!
results
You will have to find a way around that? i cant think of a way?
very glitchy i say?
results
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 ..0700797and 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 )
You will have to find a way around that? i cant think of a way?
very glitchy i say?
- Jasheppard
- Forum Newbie
- Posts: 24
- Joined: Tue Jun 17, 2008 11:44 pm
Re: problem with str_replace()
~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
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.
-- 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;
?>
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.
Last edited by Jasheppard on Thu Jun 26, 2008 2:22 am, edited 1 time in total.
-
avlisodraude
- Forum Newbie
- Posts: 2
- Joined: Wed Jun 25, 2008 7:24 am
Re: problem with str_replace()
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.
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 usedand it works. becaus i made it replace 0 with .0700 BEFORE it replaces 4 with .0797Code: 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; ?>
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.
- Jasheppard
- Forum Newbie
- Posts: 24
- Joined: Tue Jun 17, 2008 11:44 pm
Re: problem with str_replace()
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?
Unless you covert the first lot of numbers into characters then after convert the characters back to numbers.
bit confusing?