php中文字符串截取多種方法匯總
1. 截取GB2312中文字符串
<?php
< ?php
//截取中文字符串
function mysubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
?>
2. 截取utf8編碼的多字節(jié)字符串
<?php
< ?php
//截取utf8字符串
function utf8Substr($str, $from, $len)
{
return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
'$1',$str);
}
?>
3. UTF-8、GB2312都支持的漢字截取函數(shù)
<?php
< ?php
/*
Utf-8、gb2312都支持的漢字截取函數(shù)
cut_str(字符串, 截取長(zhǎng)度, 開(kāi)始長(zhǎng)度, 編碼);
編碼默認(rèn)為 utf-8
開(kāi)始長(zhǎng)度默認(rèn)為 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');
?>
4. BugFree 的字符截取函數(shù)
< ?php
/**
* @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 = "17test.info 走在中國(guó)自動(dòng)化測(cè)試的前沿";
$Length = "18";
$Append = false;
echo sysSubStr($String,$Length,$Append);
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家有所幫助,希望大家繼續(xù)關(guān)注腳本之家的最新內(nèi)容。
相關(guān)文章
PHP的serialize序列化數(shù)據(jù)以及JSON格式化數(shù)據(jù)分析
這篇文章的內(nèi)容是PHP的serialize序列化數(shù)據(jù)以及JSON格式化數(shù)據(jù)分析,需要的朋友可以參考下2015-10-10
PHP中Static(靜態(tài))關(guān)鍵字功能與用法實(shí)例分析
這篇文章主要介紹了PHP中Static(靜態(tài))關(guān)鍵字功能與用法,結(jié)合實(shí)例形式分析了Static關(guān)鍵字功能、以及靜態(tài)屬性、靜態(tài)變量等相關(guān)使用技巧,需要的朋友可以參考下2019-04-04
PHP實(shí)現(xiàn)簡(jiǎn)單的協(xié)程任務(wù)調(diào)度demo示例
這篇文章主要介紹了PHP實(shí)現(xiàn)簡(jiǎn)單的協(xié)程任務(wù)調(diào)度demo,結(jié)合實(shí)例形式詳細(xì)分析了PHP基于協(xié)程的任務(wù)調(diào)度基本原理、定義及使用技巧,需要的朋友可以參考下2020-02-02
php5.3 不支持 session_register() 此函數(shù)已啟用的解決方法
php從5.2.x升級(jí)到5.3.2.出來(lái)問(wèn)題了。有些原來(lái)能用的程序報(bào)錯(cuò)了,Deprecated: Function session_register() is deprecated2013-11-11
PHP 文件上傳進(jìn)度條的兩種實(shí)現(xiàn)方法的代碼
PHP 文件上傳進(jìn)度條的兩種實(shí)現(xiàn)方法的代碼...2007-11-11
PHP+Ajax 檢測(cè)網(wǎng)絡(luò)是否正常實(shí)例詳解
這篇文章主要介紹了PHP+Ajax 檢測(cè)網(wǎng)絡(luò)是否正常實(shí)例詳解的相關(guān)資料,這里附有實(shí)例代碼,需要的朋友可以參考下2016-12-12
PHP正則匹配日期和時(shí)間(時(shí)間戳轉(zhuǎn)換)的實(shí)例代碼
本文介紹下,用php實(shí)現(xiàn)正則匹配日期與時(shí)間,并進(jìn)行時(shí)間戳轉(zhuǎn)換的例子,有需要的朋友,參考下吧2016-12-12

