php 使字符串中的第一个字母大写,其余字母小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8735798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make first letter uppercase and the rest lowercase in a string
提问by user1048676
All, I'm trying to insert a last name into a database. I'd like the first letter to be capitalized for the name and if they have use two last names then capitalize the first and second names. So for example if someone enters:
所有,我正在尝试将姓氏插入数据库。我希望名字的第一个字母大写,如果他们使用了两个姓氏,则将第一个和第二个名字大写。例如,如果有人输入:
marriedname maidenname
婚前姓氏
It would convert this to Marriedname Maidenname and so on if there is more then two names. The other scenario is is someone has an apostrophe in their name, so is there anyway to do it if someone enters:
如果有两个以上的名字,它会将其转换为 Marriedname Maidenname 等。另一种情况是有人在他们的名字中有撇号,所以如果有人输入,无论如何都可以这样做:
o'connell
奥康奈尔
This would need to convert to O'Connell. I was using:
这需要转换为奥康奈尔。我正在使用:
ucfirst(strtolower($last_name));
However, as you can tell that wouldn't work for all the scenarios. Thanks for any advice!
但是,正如您所知,这不适用于所有场景。感谢您的任何建议!
采纳答案by Paul
This will capitalize all word's first letters, and letters immediately after an apostrophe. It will make all other letters lowercase. It should work for you:
这将大写所有单词的第一个字母,以及紧跟在撇号之后的字母。它将使所有其他字母小写。它应该适合你:
str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($last_name))));
回答by Paul
you can try this for word's
你可以试试这个
<?php echo ucwords(strtolower('Dhaka, JAMALPUR, sarishabari')) ?>
result is: Dhaka, Jamalpur, Sarishabari
结果是: Dhaka, Jamalpur, Sarishabari
回答by Antonio Max
None of these are UTF8 friendly, so here's one that works flawlessly (so far)
这些都不是 UTF8 友好的,所以这是一个完美无缺的(到目前为止)
function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"), $exceptions = array("and", "to", "of", "das", "dos", "I", "II", "III", "IV", "V", "VI"))
{
/*
* Exceptions in lower case are words you don't want converted
* Exceptions all in upper case are any words you don't want converted to title case
* but should be converted to upper case, e.g.:
* king henry viii or king henry Viii should be King Henry VIII
*/
$string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
foreach ($delimiters as $dlnr => $delimiter) {
$words = explode($delimiter, $string);
$newwords = array();
foreach ($words as $wordnr => $word) {
if (in_array(mb_strtoupper($word, "UTF-8"), $exceptions)) {
// check exceptions list for any words that should be in upper case
$word = mb_strtoupper($word, "UTF-8");
} elseif (in_array(mb_strtolower($word, "UTF-8"), $exceptions)) {
// check exceptions list for any words that should be in upper case
$word = mb_strtolower($word, "UTF-8");
} elseif (!in_array($word, $exceptions)) {
// convert to uppercase (non-utf8 only)
$word = ucfirst($word);
}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
}//foreach
return $string;
}
Usage:
用法:
$s = 'S?O JO?O DOS SANTOS';
$v = titleCase($s); // 'S?o Jo?o dos Santos'
回答by jalalkhan121
Use this built-in function:
使用这个内置函数:
ucwords('string');
回答by Steve Orman
I don't believe there will be one good answer that covers all scenarios for you. The PHP.net forum for ucwords
has a fair amount of discussions but none seem to have an answer for all. I would recommend that you standardize either using uppercase or leaving the user's input alone.
我不相信会有一个涵盖所有场景的好答案。PHP.net 论坛ucwords
有相当多的讨论,但似乎没有一个对所有人都有答案。我建议您标准化使用大写字母或单独保留用户的输入。
回答by Jonas Lundman
This is a little more simple and more direct answer to the main question. the function below mimics the PHP approachs. Just in case if PHP extend this with their namespaces in the future, a test is first checked. Im using this water proof for any languages in my wordpress installs.
这是对主要问题的更简单、更直接的回答。下面的函数模仿了 PHP 方法。以防万一 PHP 将来使用它们的命名空间扩展它,首先检查测试。我在我的 wordpress 安装中为任何语言使用了这种防水。
$str = mb_ucfirst($str, 'UTF-8', true);
This make first letter uppercase and all other lowercase as the Q was. If the third arg is set to false (default), the rest of the string is not manipulated.
这使第一个字母大写,所有其他小写字母与 Q 一样。如果第三个 arg 设置为 false(默认),则不会操作字符串的其余部分。
// Extends PHP
if (!function_exists('mb_ucfirst')) {
function mb_ucfirst($str, $encoding = "UTF-8", $lower_str_end = false) {
$first_letter = mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding);
$str_end = "";
if ($lower_str_end) {
$str_end = mb_strtolower(mb_substr($str, 1, mb_strlen($str, $encoding), $encoding), $encoding);
} else {
$str_end = mb_substr($str, 1, mb_strlen($str, $encoding), $encoding);
}
$str = $first_letter . $str_end;
return $str;
}
}
/ Lundman
/ 伦德曼
回答by Abhi Beckert
You can use preg_replace
with the e
flag (execute a php function):
你可以用preg_replace
与e
标志(执行PHP函数):
function processReplacement($one, $two)
{
return $one . strtoupper($two);
}
$name = "bob o'conner";
$name = preg_replace("/(^|[^a-zA-Z])([a-z])/e","processReplacement('', '')", $name);
var_dump($name); // output "Bob O'Conner"
Perhaps the regex pattern could be improved, but what I've done is:
也许正则表达式模式可以改进,但我所做的是:
$1
is either the beginning of line orany non-alphabetic character.$2
is any lowercase alphabetic character
$1
是行首或任何非字母字符。$2
是任何小写字母字符
We then replace both of those with the result of the simple processReplacement()
function.
然后我们用简单processReplacement()
函数的结果替换这两者。
If you've got PHP 5.3 it's probably worth making processReplacement()
an anonymous function.
如果您有 PHP 5.3,则可能值得制作processReplacement()
一个匿名函数。
回答by Fletch
First convert to title case, then find the first apostrophe and uppercase the NEXT character. You will need to add many checks, to ensure that there is a char after the apostrophe, and this code will only work on one apostrophe. e.g. "Mary O'Callahan O'connell".
首先转换为标题大小写,然后找到第一个撇号并将 NEXT 字符大写。您将需要添加许多检查,以确保撇号后有一个字符,并且此代码仅适用于一个撇号。例如“玛丽·奥卡拉汉·奥康奈尔”。
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
$pos = strpos($str, "'");
if ($pos != FALSE)
{
$str[$pos+1] = strtoupper($str[$pos+1]);
}
回答by chgav007
use like this ucfirst(strtolower($var));
像这样使用 ucfirst(strtolower($var));
回答by Irfan Dona
You can use strtolower
and ucwords
functions in PHP.
您可以在 PHP 中使用strtolower
和ucwords
函数。
First: lower all inputted text using strtolower('inputtedtext') then capitalise all text using
ucwords('strtolower')`.
首先:使用strtolower('inputtedtext') then capitalise all text using
ucwords('strtolower')`降低所有输入的文本。
Sample :
样本 :
$text = 'tHis iS sOme tEXt'; <br>
$lower = strtolower($text);   //this will lower all letter from the text <br>
$upper = ucwords($lower);   //this will uppercase all first letter from the text <br>
echo $upper;
Result = This Is Some Text
结果 = 这是一些文字
You can use one line code for this with ucwords(strtolower($text));
您可以为此使用一行代码 ucwords(strtolower($text));