title: php/regex: How to replace part of a found pattern, but leaving the rest as it is?
body: <p>How can I replace a substring in a found pattern, but leaving the rest as it is?</p>
  <p>(EDIT: The real case is of course more complicated than the example below, I have to match occurrences within xml tags. That's why I have to use regex!)</p>
  <p>Let's say I want to change occurrences of the letter &quot;X&quot; within a word to the letter &quot;Z&quot;.</p>
  <p>I want</p>
  <blockquote>
  <p>aaXaa aaX Xaa</p>
  </blockquote>
  <p>to become</p>
  <blockquote>
  <p>aaZaa aaZ Zaa</p>
  </blockquote>
  <p>Finding occurrences of words including &quot;x&quot; isn't a problem, like this:</p>
  <blockquote>
  <p>[^X\s]<em>X[^\s]</em></p>
  </blockquote>
  <p>but a normal preg_match replaces the complete match, where I want anything in the pattern except &quot;X&quot; to stay as it is.</p>
  <p>Which is the best way to accomplish this in php?</p>