Jump to content

[SOLVED] str_replace in a loop


dsaba

Recommended Posts

here's a sample code of what i'm doing in simple terms:

<?php
//queryrow = mysql_fetch_array yields 10 rows in db

$string = 'Hello my name is Bob';

while ($queryrow = mysql_fetch_array($query)) 	{
$lookingfor = $queryrow['text'];
$replacedtext = $queryrow['othertext'];
$newstring = str_replace($lookingfor, $replacedtext, $string);
								}
echo $newstring;

//newstring echoes to the last replaced string, all the previous replacements are still there
?>

 

the problem is newstring is only replaced with the last str_replace, and the string does not have all the 10 things i replaced in it

 

how I do this with this loop?

-thanks

 

 

if you dont understand my code and example

image this:

 

you have an array of the numbers, 1, 2, 3, 4, 5, 6

you have a string of "one two three four five six"

 

in the database its setup like this:

numerical    spelled_out

1                one

2                two

3                  three

4                  four

5                  five

6                  six

 

so you make a while statement for each row of the database (thats a loop that loops 6 times!)

 

you're text string is "I'm 4 years old and I ran up 6 steps!"

 

you want the text string to become:

"I'm four years old and i ran up six steps"

 

you want to do this utilizing your database where you have definitions of numerical vs. spelled-out

 

how can it be done?

Link to comment
https://forums.phpfreaks.com/topic/47704-solved-str_replace-in-a-loop/
Share on other sites

here's a sample code of what i'm doing in simple terms:

<?php
//queryrow = mysql_fetch_array yields 10 rows in db

$string = 'Hello my name is Bob';

while ($queryrow = mysql_fetch_array($query)) 	{
$lookingfor = $queryrow['text'];
$replacedtext = $queryrow['othertext'];
$newstring = str_replace($lookingfor, $replacedtext, $string);
								}
echo $newstring;

//newstring echoes to the last replaced string, all the previous replacements are still there
?>

 

everything is fine, it is doing what you ask it to, here is what I mean

 

$string = "I'm 4 years old and I ran up 6 steps!"

 

and we have what you had

in the database its setup like this:

numerical    spelled_out

1                one

2                two

3                three

4                four

5                five

6                six

 

first time through the loop, nothing, second, nothing, third nothing fourth we get

$string = "I'm 4 years old and I ran up 6 steps!"

$newstring = str_replace($lookingfor, $replacedtext, $string); // becomes "I'm four years old and I ran up 6 steps!"

 

you make no changes to $string ******important******

 

next time round nothing then the sixth we get

$newstring = str_replace($lookingfor, $replacedtext, $string); // becomes "I'm 4 years old and I ran up six steps!"

 

*******BECAUSE YOU MADE NO CHANGES TO $string IT WORKS ON THE ORIGINAL STRING AGAIN*******

 

try this

<?php
//queryrow = mysql_fetch_array yields 10 rows in db

$string = 'Hello my name is Bob';

while ($queryrow = mysql_fetch_array($query)) 	
{
$lookingfor = $queryrow['text'];
$replacedtext = $queryrow['othertext'];
$string = str_replace($lookingfor, $replacedtext, $string);
}
echo $string;

//make the changes to the original string and keep changing that
?>

wow thanks for going to explain it very well

i knew what was going on that was wrong, how i kept referencing the original string

 

i just couldn't put my finger on it on how to fix it, you did it quite simply :)

 

p.s. how did you highlight php code without putting it in the code brackets??

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.