Skip to content

Instantly share code, notes, and snippets.

@srgoogleguy
Created July 19, 2019 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srgoogleguy/1f17d1eed2f4967e71d563fe8e0f5b7b to your computer and use it in GitHub Desktop.
Save srgoogleguy/1f17d1eed2f4967e71d563fe8e0f5b7b to your computer and use it in GitHub Desktop.
Intl versions of ucfirst/ucwords
<?php
function intl_ucfirst($str)
{
$it = IntlBreakIterator::createCharacterInstance('en_US');
$it->setText($str);
$it = $it->getPartsIterator(IntlPartsIterator::KEY_SEQUENTIAL);
$strstr= "";
foreach($it as $offset => $char) {
if (!$offset) {
$char = IntlChar::toupper($char);
}
$strstr .= $char;
}
return $strstr;
}
function intl_ucwords($str)
{
$words_it = IntlBreakIterator::createWordInstance('en_US');
$words_it->setText($str);
$words_it = $words_it->getPartsIterator(IntlPartsIterator::KEY_SEQUENTIAL);
$strstr= "";
foreach($words_it as $word) {
$it = IntlBreakIterator::createCharacterInstance('en_US');
$it->setText($word);
$it = $it->getPartsIterator(IntlPartsIterator::KEY_SEQUENTIAL);
foreach ($it as $offset => $char) {
if (!$offset) {
$char = IntlChar::toupper($char);
}
$strstr .= $char;
}
}
return $strstr;
}
$str = "ãndre ǩurchavitz";
echo ucfirst($str), "\n", intl_ucfirst($str), "\n", intl_ucwords($str);
ãndre ǩurchavitz
Ãndre ǩurchavitz
Ãndre Ǩurchavitz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment