/** * Capitalizes all the words and replaces some characters in the string to create a nicer * looking title. {@link titleize()} is meant for creating pretty output. It is not used in * the Rails internals. * * <pre> * $this->titleize('man from the boondocks'); // "Man From The Boondocks" * $this->titleize('x-men: the last stand'); // "X Men: The Last Stand" * $this->titleize('TheManWithoutAPast'); // "The Man Without A Past" * $this->titleize('raiders_of_the_lost_ark'); // "Raiders Of The Lost Ark" * </pre> * * @param string $str * * @return string */ public function titleize($str) { $str = $this->underscore($str); $str = $this->humanize($str); $str = preg_replace_callback('/\\b(?<![\'’`])[[:lower:]]/u', function ($matches) { return upcase($matches[0]); }, $str); return $str; }
/** * Specifies a new irregular that applies to both pluralization and singularization at the * same time. This can only be used for strings, not regular expressions. You simply pass * the irregular in singular and plural form. * * <pre> * $this->irregular('child', 'children'); * $this->irregular('person', 'people'); * </pre> * * @param string $singular * @param string $plural */ public function irregular($singular, $plural) { unset($this->uncountables[$singular]); unset($this->uncountables[$plural]); $s0 = mb_substr($singular, 0, 1); $s0_upcase = upcase($s0); $srest = mb_substr($singular, 1); $p0 = mb_substr($plural, 0, 1); $p0_upcase = upcase($p0); $prest = mb_substr($plural, 1); if ($s0_upcase == $p0_upcase) { $this->plural("/({$s0}){$srest}\$/i", '\\1' . $prest); $this->plural("/({$p0}){$prest}\$/i", '\\1' . $prest); $this->singular("/({$s0}){$srest}\$/i", '\\1' . $srest); $this->singular("/({$p0}){$prest}\$/i", '\\1' . $srest); } else { $s0_downcase = downcase($s0); $p0_downcase = downcase($p0); $this->plural("/{$s0_upcase}(?i){$srest}\$/", $p0_upcase . $prest); $this->plural("/{$s0_downcase}(?i){$srest}\$/", $p0_downcase . $prest); $this->plural("/{$p0_upcase}(?i){$prest}\$/", $p0_upcase . $prest); $this->plural("/{$p0_downcase}(?i){$prest}\$/", $p0_downcase . $prest); $this->singular("/{$s0_upcase}(?i){$srest}\$/", $s0_upcase . $srest); $this->singular("/{$s0_downcase}(?i){$srest}\$/", $s0_downcase . $srest); $this->singular("/{$p0_upcase}(?i){$prest}\$/", $s0_upcase . $srest); $this->singular("/{$p0_downcase}(?i){$prest}\$/", $s0_downcase . $srest); } return $this; }
/** * Returns a copy of str with the first character converted to uppercase and the * remainder to lowercase. * * @param string $str */ function capitalize($str) { return upcase(mb_substr($str, 0, 1)) . downcase(mb_substr($str, 1)); }
function sku_gen($title = '', $code = false) { $prefix = ''; foreach (explode(" ", $title) as $word) { if (strlen8($word) < 3) { continue; } if (!ctype_upper($word[0])) { continue; } $prefix .= $word[0]; if (strlen($prefix) > 2) { break; } } if (!$code) { $code = substr(abs(crc32(upcase($title))), -6); } if (strlen($prefix) > 1) { $code = $prefix . '-' . $code; } return re($code); }
/** * Capitalizes the first word and turns underscores into spaces and strips a trailing "_id", * if any. Like {@link titleize()}, this is meant for creating pretty output. * * <pre> * $this->humanize('employee_salary'); // "Employee salary" * $this->humanize('author_id'); // "Author" * </pre> * * @param string $lower_case_and_underscored_word * * @return string */ public function humanize($lower_case_and_underscored_word) { $result = (string) $lower_case_and_underscored_word; foreach ($this->inflections->humans as $rule => $replacement) { $result = preg_replace($rule, $replacement, $result, 1, $count); if ($count) { break; } } $acronyms = $this->inflections->acronyms; $result = preg_replace('/_id$/', "", $result); $result = strtr($result, '_', ' '); $result = preg_replace_callback('/([a-z\\d]*)/i', function ($matches) use($acronyms) { list($m) = $matches; return !empty($acronyms[$m]) ? $acronyms[$m] : downcase($m); }, $result); $result = preg_replace_callback('/^\\w/', function ($matches) { return upcase($matches[0]); }, $result); return $result; }