preg_replace在引号后大写字母 [英] preg_replace to capitalize a letter after a quote

查看:145
本文介绍了preg_replace在引号后大写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的名字:

$str = 'JAMES "JIMMY" SMITH'

我先运行strtolower,然后运行ucwords,这将返回以下内容:

I run strtolower, then ucwords, which returns this:

$proper_str = 'James "jimmy" Smith'

我想大写第二个单词的字母,其中第一个字母是双引号.这是正则表达式.看来strtoupper无法正常工作-regexp只是返回未更改的原始表达式.

I'd like to capitalize the second letter of words in which the first letter is a double quote. Here's the regexp. It appears strtoupper is not working - the regexp simply returns the unchanged original expression.

$proper_str = preg_replace('/"([a-z])/',strtoupper('$1'),$proper_str);

有任何线索吗?谢谢!

推荐答案

使用

Use the e modifier to have the substitution be evaluated:

preg_replace('/"[a-z]/e', 'strtoupper("$0")', $proper_str)

其中$0包含整个模式的匹配项,因此"和小写字母.但这没关系,因为通过strtoupper发送时"不会更改.

Where $0 contains the match of the whole pattern, so " and the lowercase letter. But that doesn’t matter since the " doesn’t change when send through strtoupper.

这篇关于preg_replace在引号后大写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆