note 99081 modified in function.mb-convert-case by danbrown

From: Date: Tue, 27 Jul 2010 01:24:55 +0000
Subject: note 99081 modified in function.mb-convert-case by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171152@lists.php.net to get a copy of this message
This is a variation of mb_convert_case that works only for UTF-8 strings and that will not convert to lowercase anything. This avoids turning "AAA aaa" into "Aaa Aaa"; it maps "AAA aaa" into ""AAA Aaa" instead. <?php function mb_convert_case_utf8_variation($s) { $arr = preg_split("//u", $s, -1, PREG_SPLIT_NO_EMPTY); $result = ""; $mode = false; foreach ($arr as $char) { $res = preg_match( '/\\p{Mn}|\\p{Me}|\\p{Cf}|\\p{Lm}|\\p{Sk}|\\p{Lu}|\\p{Ll}|'. '\\p{Lt}|\\p{Sk}|\\p{Cs}/u', $char) == 1; if ($mode) { if (!$res) $mode = false; } elseif ($res) { $mode = true; $char = mb_convert_case($char, MB_CASE_TITLE, "UTF-8"); } $result .= $char; } return $result; } ?> --was-- This is a variation of mb_convert_case that works only for UTF-8 strings and that will not convert to lowercase anything. This avoids turning "AAA aaa" into "Aaa Aaa"; it maps "AAA aaa" into ""AAA Aaa" instead. <?php function mb_convert_case_utf8_variation($s) { $arr = preg_split("//u", $s, -1, PREG_SPLIT_NO_EMPTY); $result = ""; $mode = false; foreach ($arr as $char) { $res = preg_match( '/\\p{Mn}|\\p{Me}|\\p{Cf}|\\p{Lm}|\\p{Sk}|\\p{Lu}|\\p{Ll}|'. '\\p{Lt}|\\p{Sk}|\\p{Cs}/u', $char) == 1; if ($mode) { if (!$res) $mode = false; } elseif ($res) { $mode = true; $char = mb_convert_case($char, MB_CASE_TITLE, "UTF-8"); } $result .= $char; } return $result; } ?> See also http://stackoverflow.com/questions/3296086 http://php.net/manual/en/function.mb-convert-case.php

« previous php.notes (#171152) next »