Created
July 19, 2019 16:30
-
-
Save srgoogleguy/1f17d1eed2f4967e71d563fe8e0f5b7b to your computer and use it in GitHub Desktop.
Intl versions of ucfirst/ucwords
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ãndre ǩurchavitz | |
Ãndre ǩurchavitz | |
Ãndre Ǩurchavitz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment