PHP to capitalize all letters (including after a slash) except for certain words

心不动则不痛 提交于 2019-12-10 10:37:21

问题


I want to use PHP to clean up some titles by capitalizing each word, including those following a slash. However, I do not want to capitalize the words 'and', 'of', and 'the'.

Here are two example strings:

accounting technology/technician and bookkeeping

orthopedic surgery of the spine

Should correct to:

Accounting Technology/Technician and Bookkeeping

Orthopedic Surgery of the Spine


Here's what I currently have. I'm not sure how to combine the implosion with the preg_replace_callback.

// Will capitalize all words, including those following a slash
$major = implode('/', array_map('ucwords',explode('/',$major)));

// Is supposed to selectively capitalize words in the string
$major = preg_replace_callback("/[a-zA-Z]+/",'ucfirst_some',$major);

function ucfirst_some($match) {
   $exclude = array('and','of','the');
   if ( in_array(strtolower($match[0]),$exclude) ) return $match[0];
   return ucfirst($match[0]);
}

Right now it capitalizes all words in the string, including the ones I don't want it to.


回答1:


Well, I was going to try a recursive call to ucfirst_some(), but your code appears to work just fine without the first line. ie:

<?php
$major = 'accounting technology/technician and bookkeeping';
$major = preg_replace_callback("/[a-zA-Z]+/",'ucfirst_some',$major);
echo ucfirst($major);

function ucfirst_some($match) {
   $exclude = array('and','of','the');
   if ( in_array(strtolower($match[0]),$exclude) ) return $match[0];
   return ucfirst($match[0]);
}

Prints the desired Accounting Technology/Technician and Bookkeeping.

Your regular expression matches strings of letters already, you don't seem to need to worry about the slashes at all. Just be aware that a number or symbol [like a hyphen] in the middle of a word will cause the capitalization as well.

Also, disregard the people harping on you about your $exclude array not being complete enough, you can always add in more words as you come across them. Or just Google for a list.

It should be noted that there is no single, agreed-upon "correct" way to determing what should/should not be capitalized in this way.




回答2:


You also want to make sure if words like an and the are used at the start of a sentence that they are all caps.

Note: I can not think of any terms like this that start with of or and at the start but it is easier to fix things like that before odd data creeps into your program.

There is a code snipplet out there that I have used before at http://codesnippets.joyent.com/posts/show/716

It is referred on the php.net function page for ucwords in the comments section http://php.net/manual/en/function.ucwords.php#84920



来源:https://stackoverflow.com/questions/14103702/php-to-capitalize-all-letters-including-after-a-slash-except-for-certain-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!