php截取字符串的实例代码(支持utf-8)
分享下php中截取字符串的例子,支持utf-8格式。
1,截取字符串
<?php
$string="2006年4月我又长大了一岁!";
echo substr($string,1)."...";
//截取字符串
function SubstrGB($in,$num)
{
$pos=0;
$out="";
while($pos<strlen($in))
{
$c=substr($in,$pos,1);
if($c=="\n") break;
if(ord($c)>128)
{
$out.=$c;
$pos++;
$c=substr($in,$pos,1);
$out.=$c;
}
else
{
$out.=$c;
}
$pos++;
if($pos>=$num) break;
}
return $out;
} //by www.jbxue.com
echo SubstrGB($string,8) ;
?>
2,截取中文字符
<?php
/**
* cut_string.php
* ------------------------------
* 截取中文字符.
* edit by www.jbxue.com
*/
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;
}
}
echo "<br>".cut_str($string,8,$start=0,$code='sdf') ;
?>
php截取字符串的实例代码(支持utf-8)的更多相关文章
- JS截取字符串方法实例
// JS截取字符串可使用 substring()或者slice() // // 函数:substring() // 定义:substring(start,end)表示从start到end之间的字符串 ...
- python发送邮件的实例代码(支持html、图片、附件)
转自http://www.jb51.net/article/34498.htm 第一段代码 #!/usr/bin/python# -*- coding: utf-8 -*- import emaili ...
- C#几种截取字符串的方法(split 、Substring、Replace、remove)
C#截图字符串常用的方法有 split .Substring.Replace.remove等. split的使用: 1. Split( Char ()) 返回的字符串数组包含此实例中的子字符串(由指定 ...
- [No0000A4]DOS命令(cmd)批处理:替换字符串、截取字符串、扩充字符串、获取字符串长度
1.替换字符串,即将某一字符串中的特定字符或字符串替换为给定的字符串.举例说明其功能:========================================= @echo off set a ...
- C#中截取字符串的几种方法
1.根据单个分隔字符用split截取 例如 复制代码代码如下: string st="GT123_1"; string[] sArray=st.split("_&qu ...
- C#几种截取字符串的方法小结,需要的朋友可以参考一下
1.根据单个分隔字符用split截取 例如 复制代码 代码如下: string st="GT123_1"; string[] sArray=st.split("_&quo ...
- Android单片机与蓝牙模块通信实例代码
Android单片机与蓝牙模块通信实例代码 参考路径:http://www.jb51.net/article/83349.htm 啦啦毕业了,毕业前要写毕业设计,需要写一个简单的蓝牙APP进行交互,通 ...
- 【Go】高效截取字符串的一些思考
原文链接:https://blog.thinkeridea.com/201910/go/efficient_string_truncation.html 最近我在 Go Forum 中发现了 [SOL ...
- PHP截取字符串函数substr()函数实例用法详解
在PHP中有一项非常重要的技术,就是截取指定字符串中指定长度的字符.PHP对于字符串截取可以使用PHP预定义函数substr()函数来实现.下面就来介绍一下substr()函数的语法及其应用. sub ...
随机推荐
- 安装Laravel遇到You must enable the openssl extension to download files via https问题
刚看了一篇文章说了2014年最火的10个php框架,看到了Laravel,于是便自己试试,孰料刚安装便遇到了一个问题(由于一不小心关掉了cmd,此处无法截图显示),便是如文章标题中所说的那样,goog ...
- BootStrap2学习日记12---注册表单
<form method="" action="" class="form-horizontal"> <frameset& ...
- xcode笔记
1.Alt键的使用 2.设置捕捉所有意外断点:停在代码出错处 2015年07月27日09:52:12 3.搜索 command + F:在当前的文件中搜索 command + Shift ...
- android开发之路10(文件的读写)
1.安卓中文件的数据存储实例(将文件保存到手机自带存储空间中): ①MainActivity.java public class MainActivity extends Activity imple ...
- uva 133 The Dole Queue 双向约瑟夫环 模拟实现
双向约瑟夫环. 数据规模只有20,模拟掉了.(其实公式我还是不太会推,有空得看看) 值得注意的是两个方向找值不是找到一个去掉一个,而是找到后同时去掉. 还有输出也很坑爹! 在这里不得不抱怨下Uva的o ...
- The Story of self Parameter in Python, Demystified
转自:http://www.programiz.com/article/python-self-why If you have been programming in Python (in obj ...
- Linux逻辑卷创建
1.创建PV物理卷 [root@localhost ~]# pvcreate /dev/sda4 /dev/sda5 /dev/sda6 /dev/sda7 Physical volume “/ ...
- 【数论】UVa 11526 - H(n)
What is the value this simple C++ function will return? long long H(int n) { ; ; i <= n; i=i+ ) { ...
- 转:一个C语言实现的类似协程库(StateThreads)
http://blog.csdn.net/win_lin/article/details/8242653 译文在后面. State Threads for Internet Applications ...
- 你所不知道的z-index的用法
在开始今天的内容之前,先让我们来看下面一段代码: <style type="text/css"> #div1,#div2{ width:200px; height:20 ...