英文中规范人名、地名以及标题大小写方法

2018-08-23 10:05:00
小熊
原创
14986
摘要:我们知道,在英文中,对人名、地名、以及标题首字母大小写有严格的书写习惯,那么我们怎么处理呢?

我们知道,在英文里,对人名、地名、以及标题首字母大小写有严格的书写习惯,比如:英美的姓和各个名字的第一个名的第一个字母要大写,其余的字母小写,例如 Li Mingming。

在这里,我简单给大家介绍一下英文的 大小写规则

1、冠词都不需要大写。

2、 字母多于三个(不含三个)的介词、连词首字母要大写。比如:than, thus, like等词语在作介词或连词时需要大写。但是to, and, in, as都不需要首字母大写。

3、题目的第一个单词,不管是什么词都需要首字母大写。

4、 名词、动词、形容词、副词、代词、感叹词首字母都需要大写。比如:apple, eat, good, eagerly, he, gosh, alas。

5、星期、月份名称的第一个字母要大写,但季节第一个字母不大写。例如:Sunday星期天,August八月,winter冬天,spring春天。

6、一些大型节日名称的第一个实词的第一字母都要大写。如:Children‘s Day儿童节,NationalDay国庆节, Teachers’ Day教师节。

7、大多数的缩略词要大写。实例:CCTV(中国中央电视台), ID(身份证), CD(光盘)。

8.等等,还有很多,不在一一举例。

那么,在PHP里面,也有一些关于字母大小写的函数。比如:

1.strtolower(): 该函数将传入的字符串参数所有的字符都转换成小写,并以小定形式放回这个字符串。

<?php 
$str = "My Favorite Blog Is Www.beatmoon.com.";
$str = strtolower($str);
echo $str; // my favorite blog is www.beatmoon.com. 
?>

2.strtoupper(): 该函数的作用同strtolower函数相反,是将传入的字符参数的字符全部转换成大写,并以大写的形式返回这个字符串.用法同strtolowe()一样。

<?php 
$str = "My Favorite Blog Is Www.beatmoon.com.";
$str = strtoupper($str);
echo $str; // MY FAVORITE BLOG IS WWW.BEATMOON.COM. 
?>

3.ucfirst(): 该函数的作用是将字符串的第一个字符改成大写,该函数返回首字符大写的字符串.用法同strtolower()一样。

<?php 
$foo = 'hello world!';
$foo = ucfirst($foo);             // Hello world! 
$bar = 'HELLO WORLD!';
$bar = ucfirst($bar);             // HELLO WORLD! 
$bar = ucfirst(strtolower($bar)); // Hello world! 
?>

4.ucwords():该函数将传入的字符串的每个单词的首字符变成大写。如"beatmoon com",经过该函数处理后,将返回"Beatmoon Com"。用法同strtolower()一样。

<?php 
$foo = 'hello world!';
$foo = ucwords($foo);             // Hello World!  
$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD! 
$bar = ucwords(strtolower($bar)); // Hello World! 
?>

在实际的应用中,往往这些系统内置的函数满足不了我们的需求。比如以上列出的英语语法大小写规范,指定字符大小写等等。

在这里,我整理了一个大小写函数,基本上能满足人名、地名以及英文标题大小写需求。话不多说,先上函数。

function myUcTitle($str, $is_name=false) {
        if ($is_name) {//人名处理
            $all_uppercase = '';//需全大写的特例,以“|”分隔。
            $all_lowercase = 'De La|De Las|Der|Van De|Van Der|Vit De|Von|Or|And';//需全小写特例,以“|”分隔。
        } else {
            // 地址、文章标题以及其他情况
            $all_uppercase = 'Po|Rr|Se|Sw|Ne|Nw';  //需全大写的特例,以“|”分隔。
            $all_lowercase = 'A|And|As|By|In|Of|Or|To'; //需全小写特例,以“|”分隔。
        }
        $prefixes = 'Mc'; 
        $suffixes = "'S";
        // 大写所有的首字母,为了兼容PHP7.0+,以下preg_replace使用preg_replace_callback替代。
//        $str = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")', strtolower(trim($str)));
//        $str = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")', trim($str));
        $str = preg_replace_callback('/\\b(\\w)/', function($matches){return strtoupper($matches[1]);}, trim($str));
        if ($all_uppercase) {
            // 大写首字母缩略词 e.g. PHP
//            $str = preg_replace("/\\b($all_uppercase)\\b/e", 'strtoupper("$1")', $str);
            $str = preg_replace_callback("/\\b($all_uppercase)\\b/",  function($matches){return strtoupper($matches[1]);}, $str);
        }
        if ($all_lowercase) {
            // 小写短词 e.g. and
            if ($is_name) {
                // 全变小写
//                $str = preg_replace("/\\b($all_lowercase)\\b/e", 'strtolower("$1")', $str);
                $str = preg_replace_callback("/\\b($all_lowercase)\\b/",   function($matches){return strtolower($matches[1]);}, $str);
            } else {
                // 第一个和最后一个会变成小写 (i.e. titles)
//                $str = preg_replace("/(?<=\\W)($all_lowercase)(?=\\W)/e", 'strtolower("$1")', $str);
                $str = preg_replace_callback("/(?<=\\W)($all_lowercase)(?=\\W)/",   function($matches){return strtolower($matches[1]);}, $str);
            }
        }
        if ($prefixes) {
            // 大写某些名字的前缀 e.g 'Mc'
//            $str = preg_replace("/\\b($prefixes)(\\w)/e", '"$1".strtoupper("$2")', $str);
            $str = preg_replace_callback("/\\b($prefixes)(\\w)/", function($matches){return $matches[1].strtoupper($matches[2]);}, $str);
        }
        if ($suffixes) {
            // 小写某些词的后缀 e.g. 's
//            $str = preg_replace("/(\\w)($suffixes)\\b/e", '"$1".strtolower("$2")', $str);
            $str = preg_replace_callback("/(\\w)($suffixes)\\b/",function($matches){return $matches[1].strtolower($matches[2]);}, $str);
        }
        return $str;
    }
    e.g: 
    $title = "My favorite blog is www.beatmoon.com, which has helped me learn a lot.";
    myUcTitle($title,false);
     结果:My Favorite Blog Is Www.Beatmoon.Com, Which Has Helped Me Learn a Lot.

具体使用方法和函数介绍,在函数注释上面已经标注的很清楚了,有需要的自己看看。

文章分类
联系我们
联系人: 小熊
电话: 18037578880
Email: admin@cnsite.org
QQ: 929410000
微信: itseor
微博: itseoer
网址: www.beatmoon.com