echo substr(需要截取字符串, 起止数, 需要截取的长度);
?>
截取字符串只支持英文。如果字符串包含中文。或混排。以及文字 编码。gb2312 和 utf8 。截取方式就不一样了。
推荐使用 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
* @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 = "CodeBit.cn -- 简单、精彩、通用";
$Length = "18";
$Append = false;
echo sysSubStr($String,$Length,$Append);
?>
echo substr('123456',0,3);
?>