php中截取中文字符串的代码小结,收集了php中截

作者: 计算机网络  发布:2019-09-26

截取GB2312汉语字符串

字符串截取是叁个百般广阔的编程职责,而反复带汉语的字符串截取会平时用到。尽管简易,不过自个儿写函数完结又耗时,这里介绍八个相比好用的字符串截取函数,能够胜任基本的须要了。

 代码如下

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;
}
?>

function mysubstr($str, $start, $len) {

2. 截取utf8编码的多字节字符串

$tmpstr = "";

复制代码 代码如下:

$strlen = $start + $len;

< ?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);
}
?>

for($i = 0; $i < $strlen; $i++) {

3. UTF-8、GB2312都援助的方块字截取函数

if(ord(substr($str, $i, 1)) > 0xa0) {

复制代码 代码如下:

$tmpstr .= substr($str, $i, 2);

< ?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');
?>

$i++;

4. BugFree 的字符截取函数

} else

复制代码 代码如下:

$tmpstr .= substr($str, $i, 1);

< ?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 走在中国自动化测量试验的前方";
$Length = "18";
$Append = false;
echo sysSubStr($String,$Length,$Append);
?>

}

你可能感兴趣的稿子:

  • php截取普通话字符串函数实例
  • php截取汉语字符串不乱码的措施
  • PHP截取汉语字符串的主题材料
  • PHP中文处理汉语字符串截取(mb_substr)和收获汉语字符串字数
  • PHP中运用substr()截取字符串现身汉语乱码问题该如何做
  • php中计算中文字符串长度、截取中文字符串的函数代码
  • php中的三个国语字符串截取函数
  • php截取utf-8中文字符串乱码的消除办法
  • php中扶助多样编码的粤语字符串截取函数!
  • php自定义截取普通话字符串-utf8版

return $tmpstr;

}

?>

  1. 截取utf8编码的多字节字符串

 代码如下

< ?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);

}

?>

  1. 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');

?>

  1. BugFree 的字符截取函数 < ?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 = "111cn.net-- 简单、精彩、通用";

$Length = "18";

$Append = false;

echo sysSubStr($String,$Length,$Append);

?>

本文由金沙澳门官网送注册58发布于计算机网络,转载请注明出处:php中截取中文字符串的代码小结,收集了php中截

关键词: