英文中規範人名、地名以及標題大小寫方法
- 2018-08-23 10:05:00
- 小熊 原創
- 20051
我們知道,在英文裡,對人名、地名、以及標題首字母大小寫有嚴格的書寫習慣,比如:英美的姓和各箇名字的第一箇名的第一箇字母要大寫,其餘的字母小寫,例如 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.
具體使用方法和函數介紹,在函數註釋上麵已經標註的很清楚瞭,有需要的自己看看。
| 聯繫人: | 小熊 |
|---|---|
| 電話: | 180****8880 |
| Email: | admin@cnsite.org |
| QQ: | 929410000 |
| 微信: | itseor |
| 微博: | itseoer |
| 網址: | www.beatmoon.com |
