title: PHP preg_replace: Case insensitive match with case sensitive replacement
body: <p>I'm using preg_replace in PHP to find and replace specific words in a string, like this:</p>
  
  <pre><code>$subject = "Apple apple";
  print preg_replace('/\bapple\b/i', 'pear', $subject);
  </code></pre>
  
  <p>Which gives the result 'pear pear'.</p>
  
  <p>What I'd like to be able to do is to match a word in a case insensitive way, but respect it's case when it is replaced - giving the result 'Pear pear'.</p>
  
  <p>The following works, but seems a little long winded to me:</p>
  
  <pre><code>$pattern = array('/Apple\b/', '/apple\b/');
  $replacement = array('Pear', 'pear');
  $subject = "Apple apple";
  print preg_replace($pattern, $replacement, $subject);
  </code></pre>
  
  <p>Is there a better way to do this?</p>
  
  <p>Update: Further to an excellent query raised below, for the purposes of this task I only want to respect 'title case' - so whether or not the first letter of a word is a capital.</p>