首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Php:检测由特殊字符包围的字符串中的单个字母

Php:检测由特殊字符包围的字符串中的单个字母
EN

Stack Overflow用户
提问于 2016-02-23 03:21:10
回答 1查看 68关注 0票数 0

假设我有这些字符串

代码语言:javascript
复制
tavern-o-fallon-new-york
cut-n-shoot-texas
cut-n-shoot-texas-at-o-fellon

我想要检测-o-或任何其他模式的所有匹配项,并转换为o‘

因此,我可以将字符串转换为

代码语言:javascript
复制
Tavern O'Fallon New York
Cut'N'Shoot Texas
Cut'N'Shoot Texas At O'Fellon

我相信我可以使用一些常见的模式数组,比如O','N‘等。

EN

回答 1

Stack Overflow用户

发布于 2016-02-23 03:30:43

我将使用str_replace:

代码语言:javascript
复制
$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);

输出:

代码语言:javascript
复制
Tavern O`fallon New York
Cut`N`shoot Texas
Cut`N`shoot Texas At O`fellon

编辑:

要修复小写字母,请执行以下操作:

代码语言:javascript
复制
$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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35562056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档