/**
* 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)));
}
<?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);
?>
9条答案
按热度按时间of1yzvn41#
6ioyuze22#
你觉得下面的代码怎么样?
请注意,这也可以处理重音字符(对某些语言如法语有用)。
yh2wf1be3#
这样可以吗?
aiazj4mn4#
其他方式:
a5g8bdjr5#
nxagd54h6#
类似的东西-未经测试-为了确保我理解这个问题。
dzjeubhm7#
您可以使用“ucwords"一次性将所有单词大写,并将”inplode“和”explode“一起使用,如下所示:
yizd12fk8#
a14dhokn9#
下面是一个简单的函数,可以将字符串中的所有单词转换为标题大小写: