php str_replace static到动态id

I have the following code:

<strong>this is test1 </strong>
<strong>this is test2 </strong>
<strong>this is test3 </strong>
<strong>this is test4 </strong>

and I need to change that to:

<div id="test1"><strong>this is test1 </strong></div>
<div id="test2"><strong>this is test2 </strong></div>
<div id="test3"><strong>this is test3 </strong></div>

with str_replace or something... Any ideas? As the text is coming from CMS. I need to have ids for this text to move to the subsection when the menu item is clicked.

Try:

for ($i = 1; $i <= 4; $i++) {

$test$i = str_replace('<strong>','<div id="test'.$i.'"><strong>',
    '<strong>this is test'.$i.'</strong>');

$test$i = str_replace('</strong>','</strong></div>',
    $test$i);

}

You'll now have $test1, $test2, $test3 and $test4.

Edit: Note that PHP is serverside and you'll have to reload the page to execute PHP code.

EDIT: New code:

$text = '<strong>this is a test</strong><strong>this is a test</strong>';

function str_replacef($from, $to, $subject)
{
    $from = '/'.preg_quote($from, '/').'/';

    return preg_replace($from, $to, $subject, 1);
}

for ($i = 1; $i <= 2; $i++) {
$text = str_replacef('<strong>','<div id="'.$i.'"><strong2>',$text);
$text = str_replacef('</strong>','</strong2></div>',$text);
}

$text = str_replace('<strong2>','<strong>',$text);
$text = str_replace('</strong2>','</strong>',$text);

echo $text;

You can also use preg_replace.

I'm typing on my phone at work, but something like this and the for loop from Björns answer will do it.

preg_replace("/(<strong>.*<\/strong>)/", "<div id=test" . $i . ">$1</div>"