search
Homephp教程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;
	}
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use