我正在寻找一个现有的PHP函数正确地利用多字的职位

时间:2018-02-12 16:41:10

标签: php capitalization

我确信有人已经解决了这个问题。我在表格中有一个职称列。我想正确地将职位资本化。

For example: computer and information research scientist
Should be:   Computer and Information Research Scientist

提前致谢, 布鲁斯

2 个答案:

答案 0 :(得分:1)

好像你正在寻找标题案例,我会试试这个:

function strtotitle($title) 
// Converts $title to Title Case, and returns the result. 
{ 
// Our array of 'small words' which shouldn't be capitalised if 
// they aren't the first word. Add your own words to taste.
$smallwordsarray = array( 'of','a','the','and','an','or','nor','but','is','if','then','else','when', 'at','from','by','on','off','for','in','out','over','to','into','with' );

// Split the string into separate words
$words = explode(' ', $title);

foreach ($words as $key => $word)
{
// If this word is the first, or it's not one of our small words, capitalise it
// with ucwords().
if ($key == 0 or !in_array($word, $smallwordsarray))
$words[$key] = ucwords($word);
}

// Join the words back into a string
$newtitle = implode(' ', $words);

return $newtitle;
}

一个简单的谷歌搜索让我在sitepoint.com

上找到了这个

答案 1 :(得分:-2)

我要感谢斯图亚特指出我正确的方向。他建议我看看这里:http://php.net/manual/en/function.ucwords.php#112795 这对我来说是一个很棒的起点。 再次感谢Stuart! 布鲁斯