Completed
Push — 2.1.x ( 7ab7e8 )
by f
23:20
created

functions.php ➔ slice()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 10
nop 3
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Sets encoding for using in morphos/* functions.
4
 */
5
function set_encoding($encoding) {
6
	if (function_exists('mb_internal_encoding')) {
7
		mb_internal_encoding($encoding);
8
	} else if (function_exists('iconv_set_encoding')) {
9
		iconv_set_encoding('internal_encoding', $encoding);
10
	} else {
11
		return false;
12
	}
13
}
14
set_encoding('utf-8');
15
16
/**
17
 * Calcules count of characters in string.
18
 */
19
function length($string) {
20
	if (function_exists('mb_strlen')) {
21
		return mb_strlen($string);
22
	} else if (function_exists('iconv_strlen')) {
23
		return iconv_strlen($string);
24
	} else {
25
		return false;
26
	}
27
}
28
29
/**
30
 * Slices string like python.
31
 */
32
function slice($string, $start, $end = null) {
33
	if ($end != null) {
34
		$end -= $start;
35
	}
36
37
	if (function_exists('mb_substr')) {
38
		return $end === null ? mb_substr($string, $start) : mb_substr($string, $start, $end);
39
	} else if (function_exists('iconv_substr')) {
40
		return $end === null ? iconv_substr($string, $start) : iconv_substr($string, $start, $end);
41
	} else {
42
		return false;
43
	}
44
}
45
46
/**
47
 * Lower case.
48
 */
49
function lower($string) {
50
	if (function_exists('mb_strtolower')) {
51
		return mb_strtolower($string);
52
	} else {
53
		return false;
54
	}
55
}
56
57
/**
58
 * Upper case.
59
 */
60
function upper($string) {
61
	if (function_exists('mb_strtoupper')) {
62
		return mb_strtoupper($string);
63
	} else {
64
		return false;
65
	}
66
}
67
68
/**
69
 * Name case. (ex: Thomas Lewis)
70
 */
71
function name($string) {
72
	if (function_exists('mb_strtoupper')) {
73
		return upper(slice($string, 0, 1)).lower(slice($string, 1));
74
	} else {
75
		return false;
76
	}
77
}
78
79
/**
80
 * multiple substr_count().
81
 */
82
function chars_count($string, array $chars) {
83
	if (function_exists('mb_split')) {
84
		return count(mb_split('('.implode('|', $chars).')', $string)) - 1;
85
	} else {
86
		return false;
87
	}
88
}
89
90
function pluralize($word, $animate = false, $count) {
91
	static $plu;
92
	if ($plu === null)
93
		$plu = new RussianGeneralDeclension();
94
	//$count = $count % 10;
95
	if (in_array($count, range(2, 4))) {
96
		return $plu->getForm($word, $animate, RussianGeneralDeclension::RODIT_2);
97
	} else if ($count == 1) {
98
		return $word;
99
	} else/* if (in_array($count, range(5, 9)) || $count == 0) */{
100
		$forms = $plu->pluralizeAllDeclensions($word);
101
		return $forms[RussianGeneralDeclension::RODIT_2];
102
	}
103
}
104
105
function last_position_for_one_of_chars($string, array $chars) {
106
	if (function_exists('mb_strrpos')) {
107
		$last_position = false;
108
		foreach ($chars as $char) {
109
			if (($pos = mb_strrpos($string, $char)) !== false) {
110
				$last_position = $pos;
111
			}
112
		}
113
		if ($last_position !== false) {
114
			return mb_substr($string, $last_position);
115
		}
116
		return false;
117
	} else {
118
		return false;
119
	}
120
}
121