我有一批像这样的字符串:
tHe iPad hAS gONE ouT of STOCK
PoWER uP YOur iPhone
wHAT moDEL is YOUR aPPLE iPHOne
我想将每个单词的第一个字符大写,并将其余字符小写 - 除了任何引用iPhone
or iPad
. As in:
通过使用:
ucwords(strtolower($string));
这可以完成大部分需要的工作,但显然也可以完成iPad
and iPhone
:
The Ipad Has Gone Out Of Stock
Power Up Your Iphone
What Model Is Your Apple Iphone
我怎样才能实现以下目标:
The iPad Has Gone Out Of Stock
Power Up Your iPhone
What Model Is Your Apple iPhone
您可以使用字符串替换 http://php.net/manual/en/function.str-replace.php为了这。如果前两个参数使用数组,则可以定义一组单词和替换:
echo str_replace(['Ipad', 'Iphone'], ['iPad', 'iPhone'], ucwords(strtolower($string)));
从文档中:
如果搜索和替换是数组,则 str_replace() 从每个数组中获取一个值,并使用它们来搜索和替换主题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)