搜尋
首頁php教程PHP源码字符处理集合MST_String

php代码

<?php

class MST_String {

	const
		DET_HIGHLIGHT = &#39;%s&#39;;

	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 = &#39;...&#39;) {
		$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 = &#39;...&#39;) {
		$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 = &#39;_&#39;, $firstUpper = true) {
		if ($val == null) return $val;
		if (strpos($val, $split) === false) return ($firstUpper ? ucfirst($val) : $val);
		$val = str_replace(&#39;_&#39;, &#39; &#39;, $val);
		$val = ucwords($val);
		$val = str_replace(&#39; &#39;, null, $val);
		if (!$firstUpper)
			$val = lcfirst($val);
		return $val;
	}
	
	/**
	 * 反驼峰
	 */
	static public function tableize($val, $split = &#39;_&#39;) {
		$val = preg_replace(&#39;/([A-Z]{1})/e&#39;, &#39;\&#39;_\&#39; . strtolower(\&#39;\\1\&#39;)&#39;, $val);
		if ($val{0} == &#39;_&#39;) $val = substr($val, 1);
		return $val;
	}
	
	/**
	 * 日期转日期的unix timestamp
	 */
	static public function date2num($date) {
		$parse = date_parse($date);
		return mktime($parse[&#39;hour&#39;], $parse[&#39;minute&#39;], $parse[&#39;second&#39;], $parse[&#39;month&#39;], $parse[&#39;day&#39;], $parse[&#39;year&#39;]);
	}
	
	/**
	 * 生成字符缓存
	 */
	static public function buildStrCache($type) {
		$name = "String/{$type}";
		$file = MST_Core::getPathOf($name, MST_Core::P_LIB, &#39;.txt&#39;);
		if (is_file($file)) {
			$str = file_get_contents($file);
			$strAry = explode("\r\n", $str);
			$result = array();
			foreach ($strAry as $line) {
				$items = explode(&#39;,&#39;,$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, &#39;.php&#39;), "<?php\r\nreturn ".var_export($result, 1).";");
		}
		return false;
	}
	
	/**
	 * 读取字符缓存
	 */
	static public function getStrCache($type) {
		if (!isset(self::$_loadStr[$type])) {
			$file = &#39;String/&#39; . $type;
			self::$_loadStr[$type] = MST_Core::import($file, MST_Core::P_LIB, &#39;.php&#39;);
		}
		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(&#39; &#39;, &#39; &#39;, $content);
		$content = trim($content);
		$content = preg_replace(&#39;/([\r\n]+|[\s]{2,})/i&#39;, &#39; &#39;, $content);
		$content = MST_String::cut($content, $len);
		return $content;
	}
}

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器