PHP是否在for循环中优化多个strlen调用?

I am trying to make a PHP function that replaces all occurrences between two strings for an another given string.

I am almost sure I have accomplished what I was looking for, however while I was programming I had two versions of it. I would like you guys explain me which one is better.

Version 1:

$html = "{tag param1|param2}<adfasdfsdf>adsfasdf<adsfasdfsd>{tag param1} {tag param2} sdfsdfadsfasdf";
$needle = array('{tag ', '}');
$placeholder = 'TEST';
$lengths = array(strlen($needle[0]), strlen($needle[1]), strlen($placeholder));
$offset = 0;
while(($startpos = strpos($html, $needle[0], $offset)) !== false){
    $endpos = strpos($html, $needle[1], $startpos + $lengths[0]);
    if($endpos === false) break;
    $html = substr_replace($html, $placeholder, $startpos, $endpos - $startpos + 1);
    $offset = $startpos + $lengths[2];
}
echo $html;

Version 2:

$html = "{tag param1|param2}<adfasdfsdf>adsfasdf<adsfasdfsd>{tag param1} {tag param2} sdfsdfadsfasdf";
$needle = array('{tag ', '}');
$placeholder = 'TEST';
$offset = 0;
while(($startpos = strpos($html, $needle[0], $offset)) !== false){
    $endpos = strpos($html, $needle[1], $startpos + strlen($needle[0]));
    if($endpos === false) break;
    $html = substr_replace($html, $placeholder, $startpos, $endpos - $startpos + 1);
    $offset = $startpos + strlen($placeholder);
}
echo $html;

This code search for all {tag ...........} occurrences and replace them for TEST.

I know at this point this might be micro optimization, however I would like to learn.

Any error you see or suggestion is welcome.