Created
May 17, 2018 02:41
-
-
Save maryo/48481e00492e865143885ac8e8123aad to your computer and use it in GitHub Desktop.
Convert unicode string to camel case
This file contains hidden or 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 substring(string $string, int $start, int $length = null): string | |
{ | |
return mb_substr($string, $start, $length, 'UTF-8'); | |
} | |
function lower(string $string): string | |
{ | |
return mb_strtolower($string, 'UTF-8'); | |
} | |
function lower_first(string $string): string | |
{ | |
return lower(substring($string, 0, 1)) . substring($string, 1); | |
} | |
function capitalize(string $string): string | |
{ | |
return mb_convert_case($string, MB_CASE_TITLE, 'UTF-8'); | |
} | |
function convert_to_camel_case(string $string, bool $shouldLowerFirst = true) | |
{ | |
preg_match_all('~\p{Lu}?\p{Ll}+|\p{Lu}+(?=\p{Lu}\p{Ll})|\p{Lu}+|\p{N}+~u', $string, $matches); | |
$string = implode('', array_map('capitalize', $matches[0])); | |
return $shouldLowerFirst ? lower_first($string) : $string; | |
} | |
var_dump(convert_to_camel_case('safeHTML')); | |
var_dump(convert_to_camel_case('safe_html')); | |
var_dump(convert_to_camel_case('safe_HTML')); | |
var_dump(convert_to_camel_case('safe_Html')); | |
var_dump(convert_to_camel_case('escapeHTMLEntities')); | |
var_dump(convert_to_camel_case('escape_html_entities')); | |
var_dump(convert_to_camel_case('escape_HTML_entities')); | |
var_dump(convert_to_camel_case('escape_Html_entities')); | |
var_dump(convert_to_camel_case('escape___Html____entities')); | |
var_dump(convert_to_camel_case('XMLHttpRequest')); | |
var_dump(convert_to_camel_case('XmlHTTPRequest')); | |
var_dump(convert_to_camel_case('Příliš_ŽLUŤOUČKÝ_KŮŇ-úpěl 120ďábelských ód 10 krát za sebou')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment