1.substr(源字符串,其实位置[,长度])-截取字符串返回部分字符串

 <?php
$str ="phpddt.com";
echo substr($str, 2); //pddt.com
echo substr($str, 2,3); //pdd
echo substr($str, -2); //om 负数从结尾开始取
?>

但是当你截取中文字符串的时候很容易出现乱码,因为一个汉字是两个字节,而一个英文字母是一个字节。解决办法如下:

2.mb_substr(),使用方法和substr相同,不过要开启php.ini里面extension=php_mbstring.dll扩展,不用担心,一般的空间商

都会开启这个扩展的。

 <?php
echo mb_substr("php点点通", 1,3,"UTF-8"); //hp点
?>

网上也有很多中文字符串截取教程,实现起来比较复杂,感觉还是用php自带的函数实现起来比较好。整理的网络资料(php代码)如下:

(1)截取GB2312中文字符串

 <?php
//截取GB2312中文字符串
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;
}
echo mysubstr("php点点通", 1, 5); //php点
?>

(2)截取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);
}
echo utf8Substr("php点点通", 1, 5); //hp点点通
?>

(3)支持 utf-8、gb2312都支持的汉字截取函数

 <?php
//同时支持 utf-8、gb2312都支持的汉字截取函数 ,默认编码是utf-8
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 = "php点点通提供原创php教程";
echo cut_str($str, 8, 0); //php点点通提供...
?>

php截取字符串的更多相关文章

  1. ThinkPHP 模板substr的截取字符串函数

    ThinkPHP 模板substr的截取字符串函数在Common/function.php加上以下代码 /** ** 截取中文字符串 **/ function msubstr($str, $start ...

  2. shell编程常用的截取字符串操作

    1.          常用的字符串操作 1.1.           替换字符串:$ echo ${var/ /_}#支持正怎表达式 / /表示搜索到第一个替换,// /表示搜索到的结果全部替换. ...

  3. [No0000A4]DOS命令(cmd)批处理:替换字符串、截取字符串、扩充字符串、获取字符串长度

    1.替换字符串,即将某一字符串中的特定字符或字符串替换为给定的字符串.举例说明其功能:========================================= @echo off set a ...

  4. js中substr,substring,slice。截取字符串的区别

    substr(n1,n2) n1:起始位置(可以为负数) n2:截取长度(不可以为0,不可以为负数,可以为空) 当n1为正数时,从字符串的n1下标处截取字符串(起始位置),长度为n2. 当n1为负数时 ...

  5. Linux Shell 截取字符串

    Linux Shell 截取字符串 shell中截取字符串的方法很多 ${var#*/} ${var##*/} ${var%/*} ${var%%/*} ${var:start:len} ${var: ...

  6. SQL Server中截取字符串常用函数

    SQL Server 中截取字符串常用的函数: .LEFT ( character_expression , integer_expression ) 函数说明:LEFT ( '源字符串' , '要截 ...

  7. inux中shell截取字符串方法总结

    shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...

  8. Excel怎样从一串字符中的某个指定“字符”前后截取字符及截取字符串常用函数

    怎么样可以从一串字符中的某个指定位置的前或后截取指定个数的字符. 如:12345.6789,我要截取小数点前(或后)的3个字符.怎么样操作, 另外,怎么样从右边截取字符,就是和left()函数相反的那 ...

  9. SQL注入截取字符串函数

    在sql注入中,往往会用到截取字符串的问题,例如不回显的情况下进行的注入,也成为盲注,这种情况下往往需要一个一个字符的去猜解,过程中需要用到截取字符串.本文中主要列举三个函数和该函数注入过程中的一些用 ...

  10. Thinkphp内置截取字符串函数

    Thinkphp内置了一个可以媲美smarty的模板引擎,给我们带来了很大的方便.调用函数也一样,可以和smarty一样调用自己需要的函数,而官方也内置了一些常用的函数供大家调用. 比如今天我们说的截 ...

随机推荐

  1. topcoder SRM 610 DIV2 DivideByZero

    题目的意思是给你一组数,然后不断的进行除法(注意是大数除以小数),然后将得到的结果加入这组数种然后继续进行除法, 直到没有新添加的数为止 此题按照提议模拟即可 注意要保持元素的不同 int Count ...

  2. [知识点]平衡树之Splay

    // 此博文为迁移而来,写于2015年7月18日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6rg.html 1.前 ...

  3. ihhh题解

    10分做法: 由于空间卡得紧,所以给了10分暴力分0.0所以大家很容易就知道暴力就是线段树套ac自动机辣时间:$O((\sum |qSi| + \sum |nSi|)*log Q)$空间:$O((\s ...

  4. 【BZOJ】3621: 我想那还真是令人高兴啊

    http://www.lydsy.com/JudgeOnline/problem.php?id=3621 题意:给两个三角形,问A能否通过旋转伸缩到B. #include <bits/stdc+ ...

  5. Codeforces Beta Round #8

    A题,小小的模拟题,没看懂题意啊. #include <iostream> #include <cstdio> #include <cmath> #include ...

  6. __attribute__ 变量对齐

    http://blog.163.com/sunm_lin/blog/static/9192142200741533038695/ 一.   __attribute__ ((aligned (n))) ...

  7. 九、UINavigationController切换视图 实例

    现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 NavigationCo ...

  8. linux shell 获取进程pid

    1.通过可执行程序的程序名称 a.运行程序 b.获取进程id号 c.pidof相关知识:http://www.cnblogs.com/yunsicai/p/3675938.html 2.有些程序需要在 ...

  9. [zt]Singleton和Double-Checked Locking设计模式—UML图及代码实现

    Singleton和Double-Checked Locking设计模式,分别指的是单例模式和双重检查锁模式,它们都可以用于确保某个类只有一个对象实例化. 两个模式的区别在于:Singleton模式用 ...

  10. getElementsByClassName的兼容性

    /*----------------------------index.html------------------------------------*/ <!DOCTYPE html> ...