How can I replace a substring in a found pattern, but leaving the rest as it is?
(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!)
Let's say I want to change occurrences of the letter "X" within a word to the letter "Z".
I want
aaXaa aaX Xaa
to become
aaZaa aaZ Zaa
Finding occurrences of words including "x" isn't a problem, like this:
[^X\s]X[^\s]
but a normal preg_match replaces the complete match, where I want anything in the pattern except "X" to stay as it is.
Which is the best way to accomplish this in php?