I have a batch of strings like so:
tHe iPad hAS gONE ouT of STOCK
PoWER uP YOur iPhone
wHAT moDEL is YOUR aPPLE iPHOne
I want to capitalise the first character of each word and have the remaining characters lowercase - except any references of iPhone
or iPad
. As in:
By using:
ucwords(strtolower($string));
This can do most of what is needed but obviously also does it on iPad
and iPhone
:
The Ipad Has Gone Out Of Stock
Power Up Your Iphone
What Model Is Your Apple Iphone
How can I do achieve the below:
The iPad Has Gone Out Of Stock
Power Up Your iPhone
What Model Is Your Apple iPhone
You can use str_replace for this. If you use arrays for the first two arguments, you can define a set of words and replacements:
echo str_replace(['Ipad', 'Iphone'], ['iPad', 'iPhone'], ucwords(strtolower($string)));
From the documentation:
If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject.