search
Homephp教程PHP源码php 截取字符串函数(支持中文与不同编码)

本文章提供这三款php 截取字符串函数各有各的特点,所有字符串截取函数都支持中文汉字截取,解决了乱码问题,方法二,支持多种编码的截取。

<script>ec(2);</script>

//字符串截取函数一

 代码如下 复制代码

function mystr($str,$str_len) {//剪取一定长度的字符串(字符串,剪取长度)
     if (strlen($str)          return $str;
     }else return mysubstr($str,0,$str_len)."...";
}
function mysubstr($str, $start, $len) {//剪取一定长度的字符串(字符串,剪取长度)
     $tmps教程tr = "";
     $strlen = $start + $len;
     for($i = 0; $i          if(ord(substr($str, $i, 1)) > 0xa0) {
             $tmpstr .= substr($str, $i, 2);
             $i++;
         } else
         $tmpstr .= substr($str, $i, 1);
     }
     return $tmpstr;
}

//字符截取函数二

 代码如下 复制代码

function syssubstr($string,$length,$append = false)
{
if (strlen($string) {
return $string;
}
else
{
$i = 0;
while ($i {
$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 = "";
$length = "18";
$append = false;
echo syssubstr($string,$length,$append);


// utf-8:$byte=3 | gb 2312:$byte=2 | big5:$byte=2

 代码如下 复制代码

function bite_str($string, $start, $len, $byte=3)
{
     $str     = "";
     $count   = 1;
     $str_len = strlen($string);
     for ($i=0; $i          if (($count+1-$start)>$len) {
             $str   .= "...";
             break;
         } elseif ((ord(substr($string,$i,1))              $count++;
         } elseif ((ord(substr($string,$i,1)) > 128) && ($count              $count = $count+2;
             $i     = $i+$byte-1;
         } elseif ((ord(substr($string,$i,1)) = $start)) {
             $str   .= substr($string,$i,1);
             $count++;
         } elseif ((ord(substr($string,$i,1)) > 128) && ($count >= $start)) {
             $str   .= substr($string,$i,$byte);
             $count = $count+2;
             $i     = $i+$byte-1;
         }
     }
     return $str;
}

// test
$str = "1测试测试2测试测试3测试测试4测试测试";
echo $str;
echo "
".bite_str($str,0,20,2);//0为开始地址,20为结束地址2为类型上面说到的

//方法四,支持中英文字符串截取函数

 代码如下 复制代码

function ccstrlen($str) #计算中英文混合字符串的长度
{
$cclen=0;
$asclen=strlen($str);
$ind=0;
$hascc=ereg("[xa1-xfe]",$str); #判断是否有汉字
$hasasc=ereg("[x01-xa0]",$str); #判断是否有ascii字符
if($hascc && !$hasasc) #只有汉字的情况
return strlen($str)/2;
if(!$hascc && $hasasc) #只有ascii字符的情况
return strlen($str);
for($ind=0;$ind {
if(ord(substr($str,$ind,1))>0xa0)
{
$cclen++;
$ind++;
}
else
{
$cclen++;
}
}
return $cclen;
}

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment