Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP Based Title Case, Insert acronyms that are specific to your field/industry
<?php
// Converts to proper case, deals with some edge cases, and capitalizes many medical acronyms.
// this assumes that the title is completely uppercase or completely lowercase that we get no hints
function proper_case($title)
{
// http://www.superheronation.com/2011/08/16/words-that-should-not-be-capitalized-in-titles/#comment-1945084
$lowercase_words = array('a ', 'aboard ', 'about ', 'above ', 'across ', 'after ', 'against ', 'along ', 'amid ', 'among ', 'an ', 'and ', 'anti ', 'around ', 'as ', 'at ', 'before ', 'behind ', 'below ', 'beneath ', 'beside ', 'besides ', 'between ', 'beyond ', 'but ', 'by ', 'concerning ', 'considering ', 'despite ', 'down ', 'during ', 'except ', 'excepting ', 'excluding ', 'following ', 'for ', 'from ', 'in ', 'inside ', 'into ', 'like ', 'minus ', 'near ', 'of ', 'off ', 'on ', 'onto ', 'opposite ', 'or ', 'outside ', 'over ', 'past ', 'per ', 'plus ', 'regarding ', 'round ', 'save ', 'since ', 'so ', 'than ', 'the ', 'through ', 'to ', 'toward ', 'towards ', 'under ', 'underneath ', 'unlike ', 'until ', 'up ', 'upon ', 'versus ', 'via ', 'with ', 'within ', 'without ', 'yet'
);
// https://www.medicinenet.com/common_medical_abbreviations_and_terms/article.htm
$acronyms = array('AMI ', 'B-ALL ', 'FSH ', 'HAPE ', 'HPS ', 'IBS ', 'IDDM ', 'MDS ', 'NBCCS ', 'SIDS ', 'TSH ', 'ACL ', 'AFR ', 'ADHD ', 'ADD ', 'ADR ', 'AIDS ', 'AKA ', 'ANED ', 'ADH ', 'ARDS ', 'ARF ', 'ASCVD ', 'BKA ', 'BMP ', 'BPD ', 'BSO ', 'CABG ', 'CBC ', 'CDE ', 'CPAP ', 'COPD ', 'CVA ', 'DCIS ', 'DDX ', 'DJD ', 'DNC ', 'DNR ', 'DOE ', 'DTR ', 'DVT ', 'ETOH ', 'GOMER ', 'HRT ', 'HTN ', 'IBD ', 'ICD ', 'ICU ', 'IMP ', 'ITU ', 'IPF ', 'IVF ', 'KCL ', 'LCIS ', 'LBP ', 'LLQ ', 'LUQ ', 'MCL ', 'MVP ', 'NCP ', 'NSR ', 'ORIF ', 'PCL ', 'PERRLA ', 'PFT ', 'PERRLA ', 'PCMH ', 'PMI ', 'PMS ', 'PTH ', 'PTSD ', 'PUD ', 'RDS ', 'REB ', 'RLQ ', 'ROS ', 'RUQ ', 'SAD ', 'SOB ', 'TAH ', 'THR ', 'TKR ', 'TMJ ', 'ULN ', 'URI ', 'UTI ', 'VSS ', 'XRT'
);
$title = strtolower(' ' . $title . ' '); //make the whole string lowercase, sometimes ucwords (below) does weird stuff if everything is caps. add spaces at the start and of the title in case there are titles that are just things like 'ADHD'
$title = str_ireplace("-", "- ", $title);
$title = ucwords($title); //make all words start with an uppercase letter. including the second half of a hyphenated word ie 'long-term' changes to 'Long-Term'
$title = str_ireplace("- ", "-", $title);
$title = str_ireplace($lowercase_words, $lowercase_words, $title); //replace all words that should be lowercase with their lowercase version
$title = str_ireplace($acronyms, $acronyms, $title); //replace all medical acronyms with uppercase versions
$title = trim($title); //gets rid of spaces at start and end of title
$title = ucfirst($title); //capitalizes the first letter of the first word (done in case the first word is "a" or "the" etc)
return ($title);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.