Skip to content

Commit 41a646d

Browse files
committed
begin string helper
0 parents  commit 41a646d

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# StringHelper - хелпер для работы со строками
2+
3+
* ```StringHelper::htmlspecialchars($val)``` - статичный метод, который делает htmlspecialchars() для строк и массивов
4+
* ```StringHelper::htmlspecialchars_decode($val)``` - статичный метод, который делает htmlspecialchars_decode() для строк и массивов
5+
* ```StringHelper::generateString($length, $chars)``` - статичный метод, который возвращает сгенерированную строку нужной длины
6+
* ```StringHelper::getDeclension($value, $words)``` - статичный метод, который возвращает окончания слов при слонении. _Например: 5 товаров, 1 товар, 3 товара_
7+
* ```StringHelper::truncate($string, $length, $suffix = '...', $encoding = null)``` - возвращает обрезанный текст в $length символов
8+
* ```StringHelper::truncateWords($string, $count, $suffix = '...')``` - возвращает обрезанный текст в $length слов
9+

StringHelper.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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 = [];
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 = [];
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'): string
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 = ['', 'а', 'ов']): string
89+
{
90+
$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): string
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 = '...'): string
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+
}

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "darkfriend/php7-string",
3+
"type": "library",
4+
"description": "PHP7 debug helper",
5+
"keywords": ["dev","string","helper","darkfriend","development","php7","php"],
6+
"homepage": "https://github.com/darkfriend/php7-string",
7+
"authors": [
8+
{
9+
"name": "darkfriend",
10+
"email": "hi@darkfriend.ru"
11+
}
12+
],
13+
"require": {
14+
"php":">=7.0"
15+
},
16+
"autoload": {
17+
"classmap":[
18+
"./StringHelper.php"
19+
]
20+
}
21+
}

0 commit comments

Comments
 (0)