Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top