1+ <?php
2+
3+ namespace darkfriend \helpers ;
4+
5+ /**
6+ * This helper methods for string
7+ * @package darkfriend\helpers
8+ * @author darkfriend <hi@darkfriend.ru>
9+ * @version 1.0.0
10+ */
11+ class StringHelper
12+ {
13+ /**
14+ * Get encoded string
15+ *
16+ * @param mixed $val
17+ * @return array|string
18+ */
19+ public static function htmlspecialchars ($ val )
20+ {
21+ if (\is_array ($ val )) {
22+ $ arrReturn = array ();
23+ foreach ($ val as $ key => $ value ) {
24+ $ arrReturn [$ key ] = self ::htmlspecialchars ($ value );
25+ }
26+ return $ arrReturn ;
27+ } else {
28+ if (!empty ($ val )) {
29+ return \htmlspecialchars (
30+ $ val ,
31+ \ENT_QUOTES | \ENT_HTML5 | \ENT_DISALLOWED | \ENT_SUBSTITUTE ,
32+ 'UTF-8 '
33+ );
34+ }
35+ }
36+ return '' ;
37+ }
38+
39+ /**
40+ * Get decoded string
41+ *
42+ * @param mixed $val
43+ * @return array|string
44+ */
45+ public static function htmlspecialchars_decode ($ val )
46+ {
47+ if (\is_array ($ val )) {
48+ $ arrReturn = array ();
49+ foreach ($ val as $ key => $ value ) {
50+ $ arrReturn [$ key ] = self ::htmlspecialchars_decode ($ value );
51+ }
52+ return $ arrReturn ;
53+ } else {
54+ if ($ val ) {
55+ return \htmlspecialchars_decode (
56+ $ val ,
57+ \ENT_QUOTES | \ENT_DISALLOWED | \ENT_SUBSTITUTE
58+ );
59+ }
60+ }
61+ return '' ;
62+ }
63+
64+ /**
65+ * Get generated string
66+ *
67+ * @param int $length
68+ * @param string $chars
69+ * @return string
70+ */
71+ public static function generateString ($ length = 8 , $ chars = '0123456789ABDEFGHKNQRSTYZ ' )
72+ {
73+ $ numChars = \strlen ($ chars );
74+ $ string = '' ;
75+ for ($ i = 0 ; $ i < $ length ; $ i ++) {
76+ $ string .= \substr ($ chars , rand (1 , $ numChars ) - 1 , 1 );
77+ }
78+ return $ string ;
79+ }
80+
81+ /**
82+ * Return suffix word
83+ * @param int $value число
84+ * @param array $words массив возможных окончаний
85+ * @return string
86+ * @example 5 товаров, 1 товар, 3 товара
87+ */
88+ public static function getDeclension ($ value = 1 , $ words = array ('' , 'а ' , 'ов ' ))
89+ {
90+ $ array = array (2 , 0 , 1 , 1 , 1 , 2 );
91+
92+ return $ words [($ value % 100 > 4 && $ value % 100 < 20 ) ? 2 : $ array [($ value % 10 < 5 ) ? $ value % 10 : 5 ]];
93+ }
94+
95+ /**
96+ * Return truncated string to the number of characters specified.
97+ *
98+ * @param string $string The string to truncate.
99+ * @param int $length How many characters from original string to include into truncated string.
100+ * @param string $suffix String to append to the end of truncated string.
101+ * @param string $encoding The charset to use, defaults to charset currently used by application.
102+ * @return string the truncated string.
103+ */
104+ public static function truncate ($ string , $ length , $ suffix = '... ' , $ encoding = null )
105+ {
106+ if ($ encoding === null ) {
107+ $ encoding = 'UTF-8 ' ;
108+ }
109+
110+ if (\mb_strlen ($ string , $ encoding ) > $ length ) {
111+ return \rtrim (\mb_substr ($ string , 0 , $ length , $ encoding )) . $ suffix ;
112+ }
113+
114+ return $ string ;
115+ }
116+
117+ /**
118+ * Return truncated string to the number of words specified.
119+ *
120+ * @param string $string The string to truncate.
121+ * @param int $count How many words from original string to include into truncated string.
122+ * @param string $suffix String to append to the end of truncated string.
123+ * @return string the truncated string.
124+ */
125+ public static function truncateWords ($ string , $ count , $ suffix = '... ' )
126+ {
127+ $ words = \preg_split ('/(\s+)/u ' , \trim ($ string ), null , \PREG_SPLIT_DELIM_CAPTURE );
128+ if (\count ($ words ) / 2 > $ count ) {
129+ return \implode ('' , \array_slice ($ words , 0 , ($ count * 2 ) - 1 )) . $ suffix ;
130+ }
131+
132+ return $ string ;
133+ }
134+ }
0 commit comments