diff options
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/src/Utils/Normalizer.php')
-rw-r--r-- | www/wiki/extensions/SemanticMediaWiki/src/Utils/Normalizer.php | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/src/Utils/Normalizer.php b/www/wiki/extensions/SemanticMediaWiki/src/Utils/Normalizer.php new file mode 100644 index 00000000..bb8a5fe8 --- /dev/null +++ b/www/wiki/extensions/SemanticMediaWiki/src/Utils/Normalizer.php @@ -0,0 +1,52 @@ +<?php + +namespace SMW\Utils; + +/** + * @license GNU GPL v2+ + * @since 3.0 + * + * @author mwjames + */ +class Normalizer { + + /** + * @since 3.0 + * + * @param string $text + * + * @return string + */ + public static function toLowercase( $text ) { + return mb_strtolower( $text, mb_detect_encoding( $text ) ); + } + + /** + * @since 3.0 + * + * @param string $text + * @param integer|null $length + * + * @return string + */ + public static function reduceLengthTo( $text, $length = null ) { + + if ( $length === null || mb_strlen( $text ) <= $length ) { + return $text; + } + + $encoding = mb_detect_encoding( $text ); + $lastWholeWordPosition = $length; + + if ( strpos( $text, ' ' ) !== false ) { + $lastWholeWordPosition = strrpos( mb_substr( $text, 0, $length, $encoding ), ' ' ); // last whole word + } + + if ( $lastWholeWordPosition > 0 ) { + $length = $lastWholeWordPosition; + } + + return mb_substr( $text, 0, $length, $encoding ); + } + +} |