我正在寻找一种方法来大写字符串的第一个字母,包括用连字符连接名称的位置,例如adam smith-jones需要是Adam Smith-Jones。
ucwords()
(或ucfirst()
如果我将它们分成名字,姓氏)只有Adam Smith-jones
答案 0 :(得分:28)
$string = implode('-', array_map('ucfirst', explode('-', $string)));
答案 1 :(得分:8)
您如何看待以下代码?
mb_convert_case(mb_strtolower($value), MB_CASE_TITLE, "UTF-8");
请注意,这也处理重音字符(对于某些语言如法语有用)。
答案 2 :(得分:7)
这可以吗?
function to_upper($name)
{
$name=ucwords($name);
$arr=explode('-', $name);
$name=array();
foreach($arr as $v)
{
$name[]=ucfirst($v);
}
$name=implode('-', $name);
return $name;
}
echo to_upper("adam smith-jones");
答案 3 :(得分:4)
其他方式:
<?php
$str = 'adam smith-jones';
echo preg_replace("/(-)([a-z])/e","'\\1'.strtoupper('\\2')", ucwords($str));
?>
答案 4 :(得分:1)
/**
* Uppercase words including after a hyphen
*
* @param string $text lower-case text
* @return string Upper-Case text
*/
function uc_hyphenated_words($text)
{
return str_replace("- ","-",ucwords(str_replace("-","- ",$text)));
}
答案 5 :(得分:0)
<?php
// note - this does NOT do what you want - but I think does what you said
// perhaps you can modify it to do what you want - or we can help if you can
// provide a bit more about the data you need to update
$string_of_text = "We would like to welcome Adam Smith-jones to our 3rd, 'I am addicted to stackoverflow-posting' event.";
// both Smith-Jones and Stackoverflow-Posting should result
// may be wrong
$words = explode(' ',$string_of_text);
foreach($words as $index=>$word) {
if(false !== strpos('-',$word)) {
$parts = explode('-',$word);
$newWords = array;
foreach($parts as $wordIndex=>$part) {
$newWords[] = ucwords($part);
}
$words[$index] = implode('-',$newWords);
}
}
$words = implode(' ',$words);
?>
类似于此事 - 未经测试 - 以确保我理解这个问题。
答案 6 :(得分:0)
您可以让我们' ucwords '一次性将所有字词大写,并将“内爆”和“爆炸”组合在一起,如下所示:
ucwords(implode(" ", explode("_", "my_concatinated_word_string")));
答案 7 :(得分:0)
function capWords($string) {
$string = str_replace("-", " - ", $string);
$string = ucwords(strtolower($string));
$string = str_replace(" - ", "-", $string);
return $string;
}
答案 8 :(得分:0)
这是一个简单的函数,可以将字符串中的所有单词转换为首字母大写:
function toTitleCase($string) {
return preg_replace_callback('/\w+/', function ($match) {
return ucfirst(strtolower($match[0]));
}, $string);
}