title: How to use str_replace() to remove text a certain number of times only in PHP?
body: <p>I am trying to remove the word "John" a certain number of times from a string. I read on the php manual that str_replace excepts a 4th parameter called "count". So I figured that can be used to specify how many instances of the search should be removed. But that doesn't seem to be the case since the following:</p>
  
  <pre><code>$string = 'Hello John, how are you John. John are you happy with your life John?';
  
  $numberOfInstances = 2;
  
  echo str_replace('John', 'dude', $string, $numberOfInstances);
  </code></pre>
  
  <p>replaces all instances of the word "John" with "dude" instead of doing it just twice and leaving the other two Johns alone.</p>
  
  <p>For my purposes it doesn't matter which order the replacement happens in, for example the first 2 instances can be replaced, or the last two or a combination, the order of the replacement doesn't matter.</p>
  
  <p>So is there a way to use str_replace() in this way or is there another built in (non-regex) function that can achieve what I'm looking for?</p>