Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit fc33630

Browse files
committed
add some new methods, format codes
1 parent db1c28a commit fc33630

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

src/Json.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* Class Json
1313
* @package Toolkit\StrUtil
1414
*/
15-
class Json extends JsonHelper
15+
final class Json extends JsonHelper
1616
{
17-
1817
}

src/JsonHelper.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
*/
1515
class JsonHelper
1616
{
17+
/**
18+
* @param mixed $data
19+
* @param int $flags
20+
* @return false|string
21+
*/
22+
public static function prettyJSON(
23+
$data,
24+
int $flags = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES
25+
) {
26+
return \json_encode($data, $flags);
27+
}
28+
1729
/**
1830
* encode data to json
1931
* @param $data
@@ -40,7 +52,7 @@ public static function parse(string $data, bool $toArray = true)
4052
}
4153

4254
/**
43-
* @param string $file
55+
* @param string $file
4456
* @param bool|true $toArray
4557
* @return mixed|null|string
4658
* @throws \InvalidArgumentException

src/StringHelper.php

+29-18
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static function regexMatch(string $value, string $rule): bool
111111
];
112112

113113
$value = \trim($value);
114-
$name = \strtolower($rule);
114+
$name = \strtolower($rule);
115115

116116
// 检查是否有内置的正则表达式
117117
if (isset($validate[$name])) {
@@ -225,8 +225,8 @@ public static function random($length, array $param = []): string
225225
], $param);
226226

227227
$chars = $param['chars'];
228-
$max = \strlen($chars) - 1; //strlen($chars) 计算字符串的长度
229-
$str = '';
228+
$max = \strlen($chars) - 1; //strlen($chars) 计算字符串的长度
229+
$str = '';
230230

231231
for ($i = 0; $i < $length; $i++) {
232232
$str .= $chars[random_int(0, $max)];
@@ -261,6 +261,17 @@ public static function genUid(int $length = 7): string
261261
return \substr(\hash('md5', \uniqid('', true)), 0, $length);
262262
}
263263

264+
/**
265+
* @param string $string
266+
* @param int $indent
267+
* @param string $padStr
268+
* @return string
269+
*/
270+
public static function pad(string $string, $indent, $padStr): string
271+
{
272+
return $indent > 0 ? \str_pad($string, $indent, $padStr) : $string;
273+
}
274+
264275
/**
265276
* gen UUID
266277
* @param int $version
@@ -395,7 +406,7 @@ public static function split2Array(string $path, string $separator = '.'): array
395406

396407
/**
397408
* @param string $string
398-
* @param int $width
409+
* @param int $width
399410
* @return array
400411
*/
401412
public static function splitByWidth(string $string, int $width): array
@@ -510,10 +521,10 @@ public static function zhSubStr($str, $start = 0, $length = 0, $charset = 'utf-8
510521

511522
$slice = \mb_substr($str, $start, $length, $charset);
512523
} else {
513-
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
524+
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
514525
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
515-
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
516-
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
526+
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
527+
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
517528

518529
\preg_match_all($re[$charset], $str, $match);
519530
if (\count($match[0]) <= $length) {
@@ -557,7 +568,7 @@ public static function truncate2(string $str, int $start, int $length = null): s
557568
{
558569
if (!$length) {
559570
$length = $start;
560-
$start = 0;
571+
$start = 0;
561572
}
562573

563574
if (\strlen($str) <= $length) {
@@ -588,10 +599,10 @@ public static function truncate3(string $text, int $length = 120, array $options
588599
'html' => true
589600
];
590601

591-
$options = array_merge($default, $options);
602+
$options = array_merge($default, $options);
592603
$ellipsis = $options['ellipsis'];
593-
$exact = $options['exact'];
594-
$html = $options['html'];
604+
$exact = $options['exact'];
605+
$html = $options['html'];
595606

596607
/**
597608
* @var string $ellipsis
@@ -604,8 +615,8 @@ public static function truncate3(string $text, int $length = 120, array $options
604615
}
605616

606617
$total_length = self::strlen(strip_tags($ellipsis));
607-
$open_tags = $tags = [];
608-
$truncate = '';
618+
$open_tags = $tags = [];
619+
$truncate = '';
609620
preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
610621

611622
foreach ($tags as $tag) {
@@ -619,12 +630,12 @@ public static function truncate3(string $text, int $length = 120, array $options
619630
}
620631
}
621632
}
622-
$truncate .= $tag[1];
633+
$truncate .= $tag[1];
623634
$content_length = self::strlen(preg_replace('/&[0-9a-z]{2,8};|&#[\d]{1,7};|&#x[0-9a-f]{1,6};/i', ' ',
624635
$tag[3]));
625636

626637
if ($content_length + $total_length > $length) {
627-
$left = $length - $total_length;
638+
$left = $length - $total_length;
628639
$entities_length = 0;
629640

630641
if (preg_match_all('/&[0-9a-z]{2,8};|&#[\d]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities,
@@ -643,7 +654,7 @@ public static function truncate3(string $text, int $length = 120, array $options
643654
break;
644655
}
645656

646-
$truncate .= $tag[3];
657+
$truncate .= $tag[3];
647658
$total_length += $content_length;
648659

649660
if ($total_length >= $length) {
@@ -664,7 +675,7 @@ public static function truncate3(string $text, int $length = 120, array $options
664675
$spacepos = self::strrpos($truncate, ' ');
665676
if ($html) {
666677
$truncate_check = self::substr($truncate, 0, $spacepos);
667-
$last_open_tag = self::strrpos($truncate_check, '<');
678+
$last_open_tag = self::strrpos($truncate_check, '<');
668679
$last_close_tag = self::strrpos($truncate_check, '>');
669680

670681
if ($last_open_tag > $last_close_tag) {
@@ -801,7 +812,7 @@ public static function nameChange(string $str, bool $toCamelCase = true): string
801812
return $str;
802813
}
803814

804-
$arr_char = explode('_', strtolower($str));
815+
$arr_char = explode('_', strtolower($str));
805816
$newString = array_shift($arr_char);
806817

807818
foreach ($arr_char as $val) {

0 commit comments

Comments
 (0)