Last active
June 14, 2018 19:17
-
-
Save almino/d711d42f175890f7acdc2b69c543310d to your computer and use it in GitHub Desktop.
WordPress human name ucwords shortcode (apply correct cases for human names)
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 | |
# Include this file in your functions.php | |
add_shortcode('human-name', 'human_name'); | |
add_shortcode('human_name', 'human_name'); | |
add_shortcode('name', 'human_name'); | |
function human_name($atts, $content = null) { | |
return HumanName::parse($content); | |
} | |
class HumanName { | |
public static function exceptions() { | |
return array( | |
'da', | |
'dal', | |
'dalla', | |
'das', | |
'de', | |
'del', | |
'della', | |
'dello', | |
'der', | |
'di', | |
'do', | |
'dos', | |
'e', | |
'em', | |
'na', | |
'nas', | |
'no', | |
'nos', | |
'of', | |
'und', | |
'van', | |
'von', | |
'y', | |
); | |
} | |
public static function delimiters() { | |
# Order matters! | |
return array( | |
"O'", | |
"'", | |
".", | |
"-", | |
" ", | |
"Mc", | |
); | |
# Order matters! | |
} | |
/** | |
* ucwords for human names in Brazil | |
* Edit from http://php.net/manual/pt_BR/function.ucwords.php#112795 | |
* @param string $str | |
* @param array $delimiters | |
* @param array $exceptions Exceptions are words you don't want converted | |
* @return string | |
*/ | |
function parse($str, $delimiters = FALSE, $exceptions = FALSE) | |
{ | |
if ($delimiters === FALSE) { | |
$delimiters = self::delimiters(); | |
} | |
if ($exceptions === FALSE) { | |
$exceptions = self::exceptions(); | |
} | |
# Safe chars for preg_match | |
$preg_delimiters = array_map(function($i) { | |
return preg_quote($i); | |
}, $delimiters); | |
# Has only one word? | |
if (preg_match('/' . implode('|', $preg_delimiters) . '/', $str) !== 1) { | |
$word = mb_convert_case($str, MB_CASE_TITLE); | |
# Working with exceptions | |
if (in_array(mb_strtoupper($word), $exceptions)) { | |
$word = mb_strtoupper($word); | |
} elseif (in_array(mb_strtolower($word), $exceptions)) { | |
$word = mb_strtolower($word); | |
} elseif (preg_match('/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/', mb_strtoupper($word))) { | |
# Is roman numerals? # http://stackoverflow.com/a/267405/437459 | |
$word = mb_strtoupper($word); | |
} | |
return $word; | |
} | |
foreach ($delimiters as $delimiter) { | |
/* | |
echo '<hr />'; | |
echo "\$str = \"$str\"<br />"; | |
echo "\$delimiter = \"$delimiter\"<br />"; | |
*/ | |
# If string has a delimiter | |
if (strstr($str, $delimiter)) { | |
$ucfirst = array(); | |
# Apply ucfirst to every word | |
foreach (explode($delimiter, $str) as $word) { | |
$word = mb_convert_case($word, MB_CASE_TITLE); | |
/* | |
echo 'a word: '; | |
echo $word; | |
echo '<br />'; | |
*/ | |
# Working with exceptions | |
if (in_array(mb_strtoupper($word), $exceptions)) { | |
$word = mb_strtoupper($word); | |
} elseif (in_array(mb_strtolower($word), $exceptions)) { | |
$word = mb_strtolower($word); | |
} elseif (preg_match('/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/', mb_strtoupper($word))) { | |
# Is roman numerals? # http://stackoverflow.com/a/267405/437459 | |
$word = mb_strtoupper($word); | |
} | |
/* | |
echo 'RESULT word: '; | |
echo $word; | |
echo '<br />'; | |
*/ | |
$ucfirst[] = $word; | |
} | |
# string's first character uppercased | |
$str = implode($delimiter, $ucfirst); | |
} | |
} | |
#echo '<b>Result:</b> '; | |
return $str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment