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

Commit

Permalink
add some new methods, format codes
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 5, 2019
1 parent db1c28a commit fc33630
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* Class Json
* @package Toolkit\StrUtil
*/
class Json extends JsonHelper
final class Json extends JsonHelper
{

}
14 changes: 13 additions & 1 deletion src/JsonHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
*/
class JsonHelper
{
/**
* @param mixed $data
* @param int $flags
* @return false|string
*/
public static function prettyJSON(
$data,
int $flags = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES
) {
return \json_encode($data, $flags);
}

/**
* encode data to json
* @param $data
Expand All @@ -40,7 +52,7 @@ public static function parse(string $data, bool $toArray = true)
}

/**
* @param string $file
* @param string $file
* @param bool|true $toArray
* @return mixed|null|string
* @throws \InvalidArgumentException
Expand Down
47 changes: 29 additions & 18 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static function regexMatch(string $value, string $rule): bool
];

$value = \trim($value);
$name = \strtolower($rule);
$name = \strtolower($rule);

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

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

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

/**
* @param string $string
* @param int $indent
* @param string $padStr
* @return string
*/
public static function pad(string $string, $indent, $padStr): string
{
return $indent > 0 ? \str_pad($string, $indent, $padStr) : $string;
}

/**
* gen UUID
* @param int $version
Expand Down Expand Up @@ -395,7 +406,7 @@ public static function split2Array(string $path, string $separator = '.'): array

/**
* @param string $string
* @param int $width
* @param int $width
* @return array
*/
public static function splitByWidth(string $string, int $width): array
Expand Down Expand Up @@ -510,10 +521,10 @@ public static function zhSubStr($str, $start = 0, $length = 0, $charset = 'utf-8

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

\preg_match_all($re[$charset], $str, $match);
if (\count($match[0]) <= $length) {
Expand Down Expand Up @@ -557,7 +568,7 @@ public static function truncate2(string $str, int $start, int $length = null): s
{
if (!$length) {
$length = $start;
$start = 0;
$start = 0;
}

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

$options = array_merge($default, $options);
$options = array_merge($default, $options);
$ellipsis = $options['ellipsis'];
$exact = $options['exact'];
$html = $options['html'];
$exact = $options['exact'];
$html = $options['html'];

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

$total_length = self::strlen(strip_tags($ellipsis));
$open_tags = $tags = [];
$truncate = '';
$open_tags = $tags = [];
$truncate = '';
preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);

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

if ($content_length + $total_length > $length) {
$left = $length - $total_length;
$left = $length - $total_length;
$entities_length = 0;

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

$truncate .= $tag[3];
$truncate .= $tag[3];
$total_length += $content_length;

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

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

$arr_char = explode('_', strtolower($str));
$arr_char = explode('_', strtolower($str));
$newString = array_shift($arr_char);

foreach ($arr_char as $val) {
Expand Down

0 comments on commit fc33630

Please sign in to comment.