Forum Moderators: coopster
$contents = str_replace("a",'0',$contents); // Contents being any random sentence with multiple 'a's in itfor($i=1;$i<1000;$i++){
$contents = str_replace(($i-1),$i,$contents); // Contents being any random sentence with multiple 'a's in it
}
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.