12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace mall\utils;
- class CString {
-
- public static function msubstr($str, $length = 0, $append = true) {
- $str = trim($str);
- $strlength = strlen($str);
- if ($length == 0 || $length >= $strlength) {
- return $str;
- } elseif ($length < 0) {
- $length = $strlength + $length;
- if ($length < 0) {
- $length = $strlength;
- }
- }
- if (function_exists('mb_substr')) {
- $newstr = mb_substr($str, 0, $length);
- } elseif (function_exists('iconv_substr')) {
- $newstr = iconv_substr($str, 0, $length);
- } else {
- $newstr = substr($str, 0, $length);
- }
- if ($append && $str != $newstr) {
- $newstr .= '...';
- }
- return self::removeEmojiChar($newstr);
- }
-
- public static function removeEmpty($str) {
- $string = str_replace([" ", " "], [' ', ' '], $str);
- return trim(preg_replace("/[\r\n\t ]{1,}/", '', $string));
- }
- public static function removeEmojiChar($str){
- $mbLen = mb_strlen($str);
- $strArr = [];
- for ($i = 0; $i < $mbLen; $i++) {
- $mbSubstr = mb_substr($str, $i, 1, 'utf-8');
- if (strlen($mbSubstr) >= 4) {
- continue;
- }
- $strArr[] = $mbSubstr;
- }
- return implode('', $strArr);
- }
- }
|