summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Utils/Normalizer.php
blob: bb8a5fe8900aecefe1dbdc874ac9d7cbec556cc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 );
	}

}