使用PHP大写 [英] Capitalization using PHP

查看:108
本文介绍了使用PHP大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用PHP的名称大写方面遇到麻烦。有些名称中包含2个大写字母(例如:McCall)。当存储用于注册我们网站的用户名时,我们运行以下代码:

I am having trouble with the capitalization of names using PHP. There are some names that have 2 capital letters in them (ex: McCall). When storing a users name that registers for our website, we run the following code:

$name = ucwords(strtolower(trim($_SESSION['last_name']))) ;

这是将 mccall更改为 Mccall。我们需要的是一种方法来检查前两个字母是否以 Mc开头,如果是,则将第三个字母大写,并将名称更改为 McCall。

What this does is change 'mccall' to 'Mccall'. What we need is a way to check if the first 2 letters begin with 'Mc' and if so, the 3rd letter will be capitalized as well changing the name to 'McCall'.

非常感谢您的帮助。

推荐答案

$name = 'mccall';
$name = ucwords(strtolower(trim($name))) ;

if (strpos($name, 'Mc') === 0) {
    $name = 'Mc' . ucwords(substr($name, 2, strlen($name)));
}
echo $name; // McCall

这篇关于使用PHP大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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