字符处理集合MST_String
php代码
<?php class MST_String { const DET_HIGHLIGHT = '%s'; protected static $_loadStr = array(); /** * 获取跟定字符串的字节长度 */ static public function length($str) { return mb_strlen($str, PROJECT_ENCODE); } /** * 获取跟定字符串的字符长度 */ static public function width($str) { return mb_strwidth($str, PROJECT_ENCODE); } /** * 按字节长度截取指定字符串 */ static public function cut($str, $length, $suffix = '...') { $strLen = mb_strlen($str, PROJECT_ENCODE); $suffixLen = mb_strlen($suffix, PROJECT_ENCODE); if ($strLen <= $length || $strLen <= $suffixLen) return $str; return (mb_substr($str, 0, $length - $suffixLen, PROJECT_ENCODE)) . $suffix; } /** * 按字符长度截取指定字符串 */ static public function widthCut($str, $width, $suffix = '...') { $strWidth = mb_strwidth($str, PROJECT_ENCODE); $suffixWidth = mb_strwidth($suffix, PROJECT_ENCODE); if ($strWidth <= $width || $strWidth <= $suffixWidth) return $str; $newStr = mb_strimwidth($str, 0, $width, $suffix, PROJECT_ENCODE); return $newStr; } /** * 驼峰命名 */ static public function camelize($val, $split = '_', $firstUpper = true) { if ($val == null) return $val; if (strpos($val, $split) === false) return ($firstUpper ? ucfirst($val) : $val); $val = str_replace('_', ' ', $val); $val = ucwords($val); $val = str_replace(' ', null, $val); if (!$firstUpper) $val = lcfirst($val); return $val; } /** * 反驼峰 */ static public function tableize($val, $split = '_') { $val = preg_replace('/([A-Z]{1})/e', '\'_\' . strtolower(\'\\1\')', $val); if ($val{0} == '_') $val = substr($val, 1); return $val; } /** * 日期转日期的unix timestamp */ static public function date2num($date) { $parse = date_parse($date); return mktime($parse['hour'], $parse['minute'], $parse['second'], $parse['month'], $parse['day'], $parse['year']); } /** * 生成字符缓存 */ static public function buildStrCache($type) { $name = "String/{$type}"; $file = MST_Core::getPathOf($name, MST_Core::P_LIB, '.txt'); if (is_file($file)) { $str = file_get_contents($file); $strAry = explode("\r\n", $str); $result = array(); foreach ($strAry as $line) { $items = explode(',',$line); for ($i = 1; $i < count($items);$i++) { $result[$items[$i]] = $items[0]; } } return file_put_contents(MST_Core::getPathOf($name, MST_Core::P_LIB, '.php'), "<?php\r\nreturn ".var_export($result, 1).";"); } return false; } /** * 读取字符缓存 */ static public function getStrCache($type) { if (!isset(self::$_loadStr[$type])) { $file = 'String/' . $type; self::$_loadStr[$type] = MST_Core::import($file, MST_Core::P_LIB, '.php'); } return self::$_loadStr[$type]; } /** * 字符替换 */ static public function charReplace($str, $type) { if ($typeWords = self::getStrCache($type)) { return str_replace(array_keys($typeWords), array_values($typeWords), $str); } return $str; } /** * 生成指定字符内容的摘要信息 */ static public function summary($content, $len = 256) { $content = nl2br($content); $content = strip_tags($content); $content = str_replace(' ', ' ', $content); $content = trim($content); $content = preg_replace('/([\r\n]+|[\s]{2,})/i', ' ', $content); $content = MST_String::cut($content, $len); return $content; } }
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
刺客信條陰影:貝殼謎語解決方案
1 個月前
By DDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
3 週前
By DDD
在哪裡可以找到原子中的起重機控制鑰匙卡
1 個月前
By DDD
如何修復KB5055523無法在Windows 11中安裝?
2 週前
By DDD
Inzoi:如何申請學校和大學
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)