假设我有这些字符串
tavern-o-fallon-new-york
cut-n-shoot-texas
cut-n-shoot-texas-at-o-fellon我想要检测-o-或任何其他模式的所有匹配项,并转换为o‘
因此,我可以将字符串转换为
Tavern O'Fallon New York
Cut'N'Shoot Texas
Cut'N'Shoot Texas At O'Fellon我相信我可以使用一些常见的模式数组,比如O','N‘等。
发布于 2016-02-23 03:30:43
我将使用str_replace:
$phrase = 'tavern-o-fallon-new-york
cut-n-shoot-texas
cut-n-shoot-texas-at-o-fellon';
$phrase = str_replace(array('-n-','o-','-'),array('`N`','O`',' '), $phrase);
echo ucwords($phrase);输出:
Tavern O`fallon New York
Cut`N`shoot Texas
Cut`N`shoot Texas At O`fellon编辑:
要修复小写字母,请执行以下操作:
$phrase = str_replace(array('-n-','o-','-'),array('`N` ','O` ',' '), $phrase); // add an extra space after the quote;
$phrase = ucwords($phrase); //then ucfirst can do the job
$phrase = str_replace('` ','`', $phrase); //remove the extra space
echo $phrase;https://stackoverflow.com/questions/35562056
复制相似问题