简体   繁体   中英

Regex capitalize second letter of word

Using php, i would like to take an all lowercase word and if the first letter of the word starts with an i then capitalise the second letter.

So if my word was iphone then after the preg_replace() the string would be iPhone .

使用此正则表达式(?<=\\b\\w)(\\w)查找字母,但正则表达式无法修改字符串,使用正则表达式匹配的索引通过php大写

Try this:

$str = preg_replace('/^i([a-z])([a-z]+)$/e', '"i" . strtoupper("\\1") . "\\2"', $str);

This code checks a single word to see if it is all lowercase. If so it capitalizes the second letter.

If it gets more complex, you may want to use preg_replace_callback() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM