Forum Moderators: coopster

Message Too Old, No Replies

str replace loop add 1 to variable

         

jman11

3:11 am on Nov 14, 2009 (gmt 0)

10+ Year Member



how come this loop doesnt work?


for($i=0;$i<1000;$i++){
$contents = str_replace("a",$i,$contents); // Contents being any random sentence with multiple 'a's in it
}

it should add +1 everything it replaces but it just returns 0's for all of the 'a' replacements

TheMadScientist

3:44 am on Nov 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It changes all occurrences of 'a' to 0 the first time through...

jman11

3:51 am on Nov 14, 2009 (gmt 0)

10+ Year Member



i know that, how do i make it add 1 to each replacement though?

TheMadScientist

3:58 am on Nov 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Something like this:

$contents = str_replace("a",'0',$contents); // Contents being any random sentence with multiple 'a's in it

for($i=i;$i<1000;$i++){
$contents = str_replace(($i-1),$i,$contents); // Contents being any random sentence with multiple 'a's in it
}

jman11

4:07 am on Nov 14, 2009 (gmt 0)

10+ Year Member



lol are you srs? that made a huge loop really far down the page man. are you sure you can loop like this ive tried lots of different things, and none work. yours didnt work either :/ any other ideas?

TheMadScientist

4:13 am on Nov 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



K, I'm going to let some other people answer your questions for a while after this one... I usually code on the screen, so sometimes you have to trouble shoot and work with what I show you a bit... The problem is I typed $i=i not 1... Other than that it should do exactly what you asked for:


$contents = str_replace("a",'0',$contents); // Contents being any random sentence with multiple 'a's in it

for($i=1;$i<1000;$i++){
$contents = str_replace(($i-1),$i,$contents); // Contents being any random sentence with multiple 'a's in it
}

jman11

4:20 am on Nov 14, 2009 (gmt 0)

10+ Year Member



no i noticed you typed i, i replaced with a 1. still what i described. i know how to troubleshoot problems n such, but i just dont get how to get it to replace only the word, and not echo the whole $content with it. because then it only returns the last value

rocknbil

8:11 pm on Nov 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how do i make it add 1 to each replacement though?

What is the task here?

Do you want to replace each "a" with "a 1" or "a (number found in this block)"? (like a 1 ca 2r, etc.)

Are they only the article "a"

This is a car.

or all "a's"?

This is a car.

Case insensitive or exact match?

I think you might have to explode the characters and recompile it, or use a regexp solution.


$new_content='';
$counter=0;
$target='a';
$characters = explode('',$contents);
foreach ($characters as $char) {
$new_content .= $char;
// For case insensitive, use regexp
// if (preg_match("/^$target$/i", $char)) {
if ($char == $target) {
$counter++;
$new_content .= " $counter";
}
}
echo $new_content;

There is a way to extract the numeric index of a match using preg_replace/preg_match, that might be more graceful but am short on time at the moment, and I'm not sure what the task is here.