123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- /*
- * By adding type hints and enabling strict type checking, code can become
- * easier to read, self-documenting and reduce the number of potential bugs.
- * By default, type declarations are non-strict, which means they will attempt
- * to change the original type to match the type specified by the
- * type-declaration.
- *
- * In other words, if you pass a string to a function requiring a float,
- * it will attempt to convert the string value to a float.
- *
- * To enable strict mode, a single declare directive must be placed at the top
- * of the file.
- * This means that the strictness of typing is configured on a per-file basis.
- * This directive not only affects the type declarations of parameters, but also
- * a function's return type.
- *
- * For more info review the Concept on strict type checking in the PHP track
- * <link>.
- *
- * To disable strict typing, comment out the directive below.
- */
- declare(strict_types=1);
- function acronym(string $text): string
- {
- $parts = array_filter(mb_split("[^\w']", $text));
- if (count($parts) <= 1) {
- return "";
- }
- $parts2 = [];
- foreach ($parts as $part) {
- $first = mb_substr($part, 0, 1);
- if (mb_strlen($part) == 1) {
- $parts2[] = $first;
- continue;
- }
- $first = mb_strtoupper($first);
- $part = $first . mb_substr($part, 1);
- $part = mb_ereg_replace("[a-z]", " ", $part);
- $subs = array_filter(mb_split("[\s]", $part));
- foreach ($subs as $sub) {
- $parts2[] = $sub;
- }
- }
- $reduced = array_reduce($parts2, function($accu, $v) { return $accu . mb_substr($v, 0, 1); } );
- return mb_strtoupper($reduced);
- }
|