case coversion library
PHP does not have any convenient "built-in" means of converting a text string value to "title case" format (also known as "proper case"). The few candidate functions in official PHP extensions do not properly adhere to any known English style guide for title casing. In fact, as demonstrated below, they do nothing different than the ucwords()
function!
Take the example of the phrase i am the other one
, which SHOULD be I am the Other One
when properly title cased.
INCORRECT title casing with ucwords()
echo ucwords("i am the other one");
/* --output--
I Am The Other One
*/
The mb_convert_case()
function, part of the mbstring extension, produces the same identically incorrect results as ucwords()
.
echo mb_convert_case("i am the other one",MB_CASE_TITLE);
/* --output--
I Am The Other One
*/
The IntlBreakIterator
of the intl extension disapoints as well. Besides offering an absurdly tedious to interface to apply to this purpose, it ultimately provides incorrect results when using the IntlBreakIterator::createTitleInstance()
method (identical to using ucwords()
).
$text = "i am the other one";
$titleIterator = IntlBreakIterator::createTitleInstance("en-US");
$titleIterator->setText($text);
foreach($titleIterator as $boundary) {
if (strlen($text)-1<$boundary) break 1;
$text[$boundary] = strtoupper($text[$boundary]);
}
echo $text;
/* --output--
I Am The Other One
*/
https://github.com/katmore/case-convert
Copyright (c) 2018 D. Bird. All Rights Reserved.
Case-convert is copyrighted free software. You may redistribute and modify it under either the terms and conditions of the "The MIT License (MIT)"; or the terms and conditions of the "GPL v3 License". See LICENSE and GPLv3.