convert: ucwords in UTF-8 with delimiter

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3769 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2017-06-14 11:25:59 +00:00
parent 46db7a0cf0
commit 24b5f7f12a
2 changed files with 58 additions and 1 deletions

View File

@@ -48,7 +48,7 @@ class convert
}
/** Convert the first char to capital and the rest of the sentence in
* lowercase (like ucfirst, but UTF8 compliant
* lowercase (like ucfirst, but UTF8 compliant)
* @param string $str The string to convert
*/
public static function ucfirst ($str)
@@ -56,4 +56,25 @@ class convert
$a = mb_strtoupper (mb_substr ($str, 0, 1, 'UTF-8'), 'UTF-8');
return $a . mb_substr ($str, 1, null, 'UTF-8');
}
/** Convert the first char of each word of a sentence to capital. The word
* delimiter can be provided.
* The sentence is in UTF-8.
* The sentence is converted to lowercase before doing the action (in
* contrary of the original PHP function)
* @param string $str The string to convert
* @param string $delimiters The delimiters (by default " \t\r\n\f\v")
* @return string
*/
public static function ucwords ($str, $delimiters = " \t\r\n\f\v")
{
$str = mb_strtolower ($str, "utf-8");
$res = "";
foreach (preg_split ("#([".preg_quote ($delimiters)."]+)#", $str, -1,
PREG_SPLIT_DELIM_CAPTURE) as $tok)
{
$res .= \convert::ucfirst ($tok);
}
return $res;
}
}