Home > Backend Development > PHP Tutorial > PHP string interception function_PHP tutorial

PHP string interception function_PHP tutorial

WBOY
Release: 2016-07-20 11:03:06
Original
983 people have browsed it

This article introduces a function that comes with PHP to intercept strings. It can only handle English. Numbers cannot intercept mixed Chinese characters. Friends in need can refer to it. The latter one is easier to use. The first one is mainly for For beginners to learn from. ​

 代码如下 复制代码
/*
------------------------------------------------------
参数:
$str_cut    需要截断的字符串
$length     允许字符串显示的最大长度

程序功能:截取全角和半角(汉字和英文)混合的字符串以避免乱码
------------------------------------------------------
*/
function substr_cut($str_cut,$length)
{
    if (strlen($str_cut) > $length)
    {
        for($i=0; $i < $length; $i++)
if (ord($str_cut[$i]) > 128)    $i++;
        $str_cut = substr($str_cut,0,$i)."..";
    }
    return $str_cut;
}
?>

/*
-------------------------------------------------- ----
Parameters:
$str_cut The string that needs to be truncated
$length The maximum length allowed for string display
Program function: intercept mixed full-width and half-width (Chinese characters and English) strings to avoid garbled characters
-------------------------------------------------- ----
*/
function substr_cut($str_cut,$length)
{
If (strlen($str_cut) > $length)
{
for($i=0; $i < $length; $i++)
If (ord($str_cut[$i]) > 128) $i++;
          $str_cut = substr($str_cut,0,$i)."..";
}
Return $str_cut;
}
?>

http://www.bkjia.com/PHPjc/445327.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445327.htmlTechArticle
This article introduces a function that comes with PHP to intercept strings. It can only handle English, but not numbers. Intercepted Chinese mixed arrangement, friends in need can refer to it, the latter one is easier to use,...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template