update to use preg_replace_callback

This commit is contained in:
Nathan Coad
2018-01-18 17:37:35 +11:00
parent b25a858054
commit c29d8a7a21

View File

@@ -37,25 +37,30 @@ function _booking_ucname($string) {
$suffixes = "'S"; $suffixes = "'S";
// captialize all first letters // captialize all first letters
$string = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")', strtolower(trim($string))); //$string = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")', strtolower(trim($string)));
$string = preg_replace_callback('/\\b(\\w)/', function ($m) { return strtoupper($m[1]); }, strtolower(trim($string)));
if ($all_uppercase) { if ($all_uppercase) {
// capitalize acronymns and initialisms e.g. PHP // capitalize acronymns and initialisms e.g. PHP
$string = preg_replace("/\\b($all_uppercase)\\b/e", 'strtoupper("$1")', $string); //$string = preg_replace("/\\b($all_uppercase)\\b/e", 'strtoupper("$1")', $string);
$string = preg_replace_callback('/\\b($all_uppercase)\\b/', function ($m) { return strtoupper($m[1]); }, $string);
} }
if ($all_lowercase) { if ($all_lowercase) {
// decapitalize short words e.g. and // decapitalize short words e.g. and
// all occurences will be changed to lowercase // all occurences will be changed to lowercase
$string = preg_replace("/\\b($all_lowercase)\\b/e", 'strtolower("$1")', $string); //$string = preg_replace("/\\b($all_lowercase)\\b/e", 'strtolower("$1")', $string);
$string = preg_replace_callback('/\\b($all_lowercase)\\b/', function ($m) { return strtolower($m[1]); }, $string);
} }
if ($prefixes) { if ($prefixes) {
// capitalize letter after certain name prefixes e.g 'Mc' // capitalize letter after certain name prefixes e.g 'Mc'
$string = preg_replace("/\\b($prefixes)(\\w)/e", '"$1".strtoupper("$2")', $string); //$string = preg_replace("/\\b($prefixes)(\\w)/e", '"$1".strtoupper("$2")', $string);
$string = preg_replace_callback('/\\b($prefixes)(\\w)/', function ($m) { return $m[1] . strtoupper($m[2]); }, $string);
} }
if ($suffixes) { if ($suffixes) {
// decapitalize certain word suffixes e.g. 's // decapitalize certain word suffixes e.g. 's
$string = preg_replace("/(\\w)($suffixes)\\b/e", '"$1".strtolower("$2")', $string); //$string = preg_replace("/(\\w)($suffixes)\\b/e", '"$1".strtolower("$2")', $string);
$string = preg_replace_callback('/(\\w)($suffixes)\\b/', function ($m) { return $m[1] . strtolower($m[2]); }, $string);
} }
/* /*