I'm using preg_replace in PHP to find and replace specific words in a string, like this:
$subject = "Apple apple";
print preg_replace('/\bapple\b/i', 'pear', $subject);
Which gives the result 'pear pear'.
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'.
The following works, but seems a little long winded to me:
$pattern = array('/Apple\b/', '/apple\b/');
$replacement = array('Pear', 'pear');
$subject = "Apple apple";
print preg_replace($pattern, $replacement, $subject);
Is there a better way to do this?
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.
You could do this with
preg_replace_callback
, but that's even more long winded:This code just looks at the capitalization of the first character of the match to determine what to replace with; you could adapt the code to do something more involved instead.
I have in mind this implementation for common case:
-it's more complicated, of course, but fit original request for any position. If you're looking for only first letter, this could be an overkill (see @Jon's answer then)
This is the solution that I used:
In the best words that I can I'll try explain why this works: Wrapping your search term with
()
means I want to access this value later. As it is the first item in pars in the RegEx, it is accessible with$1
, as you can see in the substitution parameter