Instead of having to write the lower- and uppercase version of each word you want to exclude respectively and thus having to write them twice, you could only define them once in an array and using str_ireplace
instead of str_replace
like this:
$string = "tHe IPHONE and iPad hAS gONE ouT of STOCK";
$excludedWords = array(
"iPad",
"iPhone"
);
echo str_ireplace($excludedWords, $excludedWords, ucwords(strtolower($string)));
Which would result in
The iPhone And iPad Has Gone Out Of Stock
This would then replace all occurrences of these words with the version you've defined in the array.
Edit:
Keep in mind that using this, words like "shipadvertise" would be replaced with "shiPadvertise".
If you want to prevent this, you could use a more advanced solution using regular expressions:
$string = "tHe IPHONE and shipadvertise iPad hAS gONE ouT of STOCK";
$excludedWords = array(
"iPad",
"iPhone"
);
$excludedWordsReg = array_map(function($a) { return '/(?<=[\s\t\r\n\f\v])'.preg_quote($a).'/i'; }, $excludedWords);
echo preg_replace($excludedWordsReg, $excludedWords, ucwords(strtolower($string)));
This would then correctly resolve into
The iPhone And Shipadvertise iPad Has Gone Out Of Stock
I've used the delimiters for determining words ucwords uses by default.