PPH substr截取函数

中文截取2,单字节截取模式

<?php
/**
 * 中文截取2,单字节截取模式
 * by hkshadow
 * 2011-07-03
 */
function SubstrCn($str, $slen, $startdd = 0) {
	global $cfg_soft_lang;
	if ($cfg_soft_lang == 'utf-8') {
		return cn_substr_utf8 ( $str, $slen, $startdd );
	}
	$restr = '';
	$c = '';
	$str_len = strlen ( $str );
	if ($str_len < $startdd + 1) {
		return '';
	}
	if ($str_len < $startdd + $slen || $slen == 0) {
		$slen = $str_len - $startdd;
	}
	$enddd = $startdd + $slen - 1;
	for($i = 0; $i < $str_len; $i ++) {
		if ($startdd == 0) {
			$restr .= $c;
		} else if ($i > $startdd) {
			$restr .= $c;
		}
		
		if (ord ( $str [$i] ) > 0x80) {
			if ($str_len > $i + 1) {
				$c = $str [$i] . $str [$i + 1];
			}
			$i ++;
		} else {
			$c = $str [$i];
		}
		
		if ($i >= $enddd) {
			if (strlen ( $restr ) + strlen ( $c ) > $slen) {
				break;
			} else {
				$restr .= $c;
				break;
			}
		}
	}
	return $restr;
}
?>

utf-8中文截取,单字节截取模式

<?
/**
 * utf-8中文截取,单字节截取模式
 * by hkshadow
 * 2011-07-03
 */
function cn_substr_utf8($str, $length, $start = 0) {
	if (strlen ( $str ) < $start + 1) {
		return '';
	}
	preg_match_all ( "/./su", $str, $ar );
	$str = '';
	$tstr = '';
	
	//为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取
	for($i = 0; isset ( $ar [0] [$i] ); $i ++) {
		if (strlen ( $tstr ) < $start) {
			$tstr .= $ar [0] [$i];
		} else {
			if (strlen ( $str ) < $length + strlen ( $ar [0] [$i] )) {
				$str .= $ar [0] [$i];
			} else {
				break;
			}
		}
	}
	return $str;
}
?>

Utf-8、gb2312都支持的汉字截取函数

<?php
/* 
Utf-8、gb2312都支持的汉字截取函数 
cut_str(字符串, 截取长度, 开始长度, 编码); 
编码默认为 utf-8 
开始长度默认为 0 
*/

function Cut_Str($string, $sublen, $start = 0, $code = 'UTF-8') {
	if ($code == 'UTF-8') {
		$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
		preg_match_all ( $pa, $string, $t_string );
		
		if (count ( $t_string [0] ) - $start > $sublen)
			return join ( '', array_slice ( $t_string [0], $start, $sublen ) ) . "...";
		return join ( '', array_slice ( $t_string [0], $start, $sublen ) );
	} else {
		$start = $start * 2;
		$sublen = $sublen * 2;
		$strlen = strlen ( $string );
		$tmpstr = '';
		
		for($i = 0; $i < $strlen; $i ++) {
			if ($i >= $start && $i < ($start + $sublen)) {
				if (ord ( substr ( $string, $i, 1 ) ) > 129) {
					$tmpstr .= substr ( $string, $i, 2 );
				} else {
					$tmpstr .= substr ( $string, $i, 1 );
				}
			}
			if (ord ( substr ( $string, $i, 1 ) ) > 129)
				$i ++;
		}
		if (strlen ( $tmpstr ) < $strlen)
			$tmpstr .= "...";
		return $tmpstr;
	}
}

//$str = "abcd需要截取的字符串"; 
//echo Cut_Str($str, 8, 0, 'gb2312'); 
?>

BugFree 的字符截取函数

<?php
/** 
 * BugFree 的字符截取函数 
 * @package     BugFree 
 * @version     $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $ 
 * Return part of a string(Enhance the function substr()) 
 * @author                  Chunsheng Wang <wwccss@263.net> 
 * @param string  $String  the string to cut. 
 * @param int     $Length  the length of returned string. 
 * @param booble  $Append  whether append "...": false|true 
 * @return string           the cutted string. 
 */

function SysSubStr($String, $Length, $Append = false) { 
    if (strlen($String) <= $Length )
	{
		return $String;
	}else
	{
		$I = 0;
		while ( $I < $Length ) {
			$StringTMP = substr ( $String, $I, 1 );
			if (ord ( $StringTMP ) >= 224) {
				$StringTMP = substr ( $String, $I, 3 );
				$I = $I + 3;
			} elseif (ord ( $StringTMP ) >= 192) {
				$StringTMP = substr ( $String, $I, 2 );
				$I = $I + 2;
			} else {
				$I = $I + 1;
			}
			$StringLast [] = $StringTMP;
		}
		$StringLast = implode ( "", $StringLast );
		if ($Append) {
			$StringLast .= "...";
		}
		return $StringLast;
	}
}

//$String = "www.mudbest.com -- 影子";
//$Length = "18";
//$Append = false;
//echo SysSubStr ( $String, $Length, $Append );
?>

Leave a Reply

(will not be published)