Hey! This is clause 1; this is clause 2. Be clause 3. I think (clause 4) that you are a fool (clause 5) and you deserve to die (clause 6).
See below for source.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-us">
  <head>
  <title>str_replace_dynamic($needle, $function, $haystack)</title>
  </head>
  <body>
<?php
function increase(){static $x=1; return $x++;}

function str_replace_dynamic($needle, $function, $haystack){
  $splitStr = explode($needle, $haystack);
  $reconstructedStr = $splitStr[0];
  foreach ( $splitStr as $str )
  {
   if($reconstructedStr==$str) continue; //skip the first one
   $reconstructedStr .= $function() . $str;
  } 
  return $reconstructedStr;
}
$sometext='Hey!  This is clause %n; this is clause %n.  
Be clause %n.  I think (clause %n) that you are a fool
(clause %n) and you deserve to die (clause %n).';

print str_replace_dynamic('%n','increase',$sometext).nl2br("\n")
  ."See below for source.<br /><br />";


highlight_file(__FILE__);
?>
  </body>
</html>