FORMATTING ROMAN NUMERALS AND TITLE CAPITALIZATIONS - Jul 9th, 2014
|
Formatting of Roman Numerals and title capitalization can be tricky, especially when you want most of the words in your title to be capitalized.
This RemoveShouting function will capitalize all roman numerals between 1 and 10 in a string (which I called $instruction) and also force all the words in the $lower_exception array to appear as lower case in that string. All other words in the string will have their first letter capitalized. The code is based on an example that I found in the PHP manual at:
http://www.php.net/manual/en/function.ucfirst.php
Just insert the function code in the body of your viewer before you need to call it, and outside of any foreach loops (you can only declare a function once on a viewer).
Here’s the function code:
<?php function RemoveShouting($instruction) { $lower_exceptions = array( "to" => "1", "a" => "1", "the" => "1", "and" => "1", "but" => "1", "or" => "1", "for" => "1", "nor" => "1", "of" => "1" ); $higher_exceptions = array( "I" => "1", "II" => "1", "III" => "1", "IV" => "1", "V" => "1", "VI" => "1", "VII" => "1", "VIII" => "1", "IX" => "1", "X" => "1" );
$words = explode(" ", $instruction); $newwords = array();
foreach ($words as $word) { $firstElement = ($word == reset($words)); $lastElement = ($word == end($words)); if (!$higher_exceptions[$word]) {$word = strtolower($word);} if ((!$lower_exceptions[$word]) || ($word == $firstElement) || ($word == $lastElement) ) {$word = ucfirst($word);} array_push($newwords, $word); } return join(" ", $newwords); } ?>
Then where you want to echo the re-formatted $instruction variable, insert:
<?php echo RemoveShouting($instruction) ?>
You can add any words to the $lower_exceptions array to format your titles, but the rules for capitalization vary between sources. Just pick one and be consistent. You can also add Roman Numerals to the $higher_exceptions array.
If you need to implement RemoveShouting more than once, rename a copy of the function and insert your new variable.
|
The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact
Interactive Tools for information on the downloading of the software or the purchasing of licenses.