Collected string interception functions in php_PHP tutorial
WBOYOriginal
2016-07-20 11:10:40937browse
This article collects several commonly used string interception functions. This solves the problem of garbled characters in various encodings in PHP. Students in need can refer to them.
3. Both UTF-8 and GB2312 are supported Chinese character interception function<🎜>/*<🎜><🎜>Chinese character interception function supported by Utf-8 and gb2312<🎜><🎜>cut_str(string, interception length, start length, encoding);<🎜><🎜>The encoding defaults to utf-8<🎜><🎜>The starting length defaults to 0<🎜><🎜>*/<🎜>
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 = "php100.com-- 简单、精彩、通用";
$Length = "18";
$Append = false;
echo sysSubStr($String,$Length,$Append);
?>
复制代码
/** * @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.
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