<?php
/*
@UTF-8编码的字符可能由1~3个字节组成。 */
/*--------------------------方法一截取中文字符串方法------------------------------*/
function msubstr($str, $start, $len)
{
$tmpstr = "";
$strlen = $start + $len;
for ($i = 0; $i < $strlen; $i++) {
if (ord(substr($str, $i, 1)) > 0xa0) //ord()函数返回字符串的第一个字符的ASCII值
{
$tmpstr .= substr($str, $i, 2);
$i++;
} else {
$tmpstr .= substr($str, $i, 1);
}
}
return $tmpstr;
} /*----------------------------第二种方法-----------------------------------*/
//截取的是UTF-8字符串
function utf_substr($str, $len)
{
$new_str = [];
for ($i = 0; $i < $len; $i++) {
$tem_str = substr($str, 0, 1);
if (ord($tem_str > 127)) {
$i++;
if ($i < $len) {
$new_str[] = substr($str, 0, 3);
$str = substr($str, 3);
}
} else {
$new_str[] = substr($str, 0, 1);
$str = substr($str, 1);
}
}
return join($new_str);//join()函数把数组元素组合为一个字符串
} /*-------------------------------------第三种方法(UTF-8)--------------------------------*/
function cutstr($string, $length)
{
preg_match_all("/[\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]/", $string, $info);
$wordscut = "";
$j = 0;
for ($i = 0; $i < count($info[0]); $i++) {
$wordscut .= $info[0][$i];
$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
if ($j > $length - 3) {
return $wordscut . "...";
}
}
return join('', $info[0]);
} $string = "312哈哈,这个组合很难切割哦";
echo cutstr($string, 10); /*---------------------------------下面是曾经用过的截取第三个的字符串的------------------------------*/
// $name1 = mysql_result($my_rst,0,"name");
// $name = preg_match("/([1-9][0-9]+)/",$name1,$r);
// $name = $r[0];
// if($name == ""){
// $name=preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,2}'.
// '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,1}).*#s',
// '$1',$name1);
// } /*--------------------------------------------第四种方法(UTF-8)---------------------------------------------*/
function cut_str($sourcestr, $cutlength)
{
$returnstr = '';
$i = 0;
$n = 0;
$str_length = strlen($sourcestr);//字符串的字节数
while ($n < $cutlength && $i <= $str_length) {
$temp_str = substr($sourcestr, $i, 1);
$ascnum = ord($temp_str);//得到字符串中第$i位字符的ASCII码
if ($ascnum >= 224) {
$returnstr = $returnstr . substr($sourcestr, $i, 3);//根据UTF-8编码规范,将3个连续的字符计为单个字符
$i = $i + 3;//实际Byte记为3
$n++;//字串长度为1
} elseif ($ascnum >= 192)//如果ASCII位高于192
{
$returnstr = $returnstr . substr($sourcestr, $i, 2);//根据UTF-8编码规范,将2个连续的字符记为单个字符
$i = $i + 2;//实际Byte记为2
$n++;//字串长度为1
} elseif ($ascnum >= 65 && $ascnum <= 90)//如果是大写字母
{
$returnstr = $returnstr . substr($sourcestr, $i, 1);
$i = $i + 1;//byte记为1
$n++;//但考虑到整体美观,大写字母计成一个高位字符
} else {
$returnstr = $returnstr . substr($sourcestr, $i, 1);
$i = $i + 1;//实际的Byte记为1
$n = $n + 0.5;//小写字母和半角标点等与半个高位字符宽...
}
}
if ($str_length > $cutlength) {
$returnstr = $returnstr . "...";//超过长度时在尾处加上省略号
}
return $returnstr;
} /*--------------------第五种方法(UTF-8)---------------------------------------------*/ function FSubstr($title, $start, $len = "", $magic = true)
{
if ($len == "") $len = strlen($title); if ($start != 0) {
$startv = ord(substr($title, $start, 1));
if ($startv >= 128) {
if ($startv < 192) {
for ($i = $start - 1; $i > 0; $i--) {
$tempv = ord(substr($title, $i, 1));
if ($tempv >= 192) break;
}
$start = $i;
}
}
} if (strlen($title) <= $len) return substr($title, $start, $len); $alen = 0;
$blen = 0;
$realnum = 0;
$length = 0;
for ($i = $start; $i < strlen($title); $i++) {
$ctype = 0;
$cstep = 0; $cur = substr($title, $i, 1);
if ($cur == "&") {
if (substr($title, $i, 4) == "&lt;") {
$cstep = 4;
$length += 4;
$i += 3;
$realnum++;
if ($magic) {
$alen++;
}
} elseif (substr($title, $i, 4) == "&gt;") {
$cstep = 4;
$length += 4;
$i += 3;
$realnum++;
if ($magic) {
$alen++;
}
} elseif (substr($title, $i, 5) == "&amp;") {
$cstep = 5;
$length += 5;
$i += 4;
$realnum++;
if ($magic) {
$alen++;
}
} elseif (substr($title, $i, 6) == "&quot;") {
$cstep = 6;
$length += 6;
$i += 5;
$realnum++;
if ($magic) {
$alen++;
}
} elseif (preg_match("/&#(\d+);?/i", substr($title, $i), $match)) {
$cstep = strlen($match[0]);
$length += strlen($match[0]);
$i += strlen($match[0]) - 1;
$realnum++;
if ($magic) {
$blen++;
$ctype = 1;
}
}
} else {
if (ord($cur) >= 252) {
$cstep = 6;
$length += 6;
$i += 5;
$realnum++;
if ($magic) {
$blen++;
$ctype = 1;
}
} elseif (ord($cur) >= 248) {
$cstep = 5;
$length += 5;
$i += 4;
$realnum++;
if ($magic) {
$ctype = 1;
$blen++;
}
} elseif (ord($cur) >= 240) {
$cstep = 4;
$length += 4;
$i += 3;
$realnum++;
if ($magic) {
$blen++;
$ctype = 1;
}
} elseif (ord($cur) >= 224) {
$cstep = 3;
$length += 3;
$i += 2;
$realnum++;
if ($magic) {
$ctype = 1;
$blen++;
}
} elseif (ord($cur) >= 192) {
$ctype = 2;
$length += 2;
$i += 1;
$realnum++;
if ($magic) {
$blen++;
$ctype = 1;
}
} elseif (ord($cur) >= 128) {
$length += 1;
} else {
$cstep = 1;
$length += 1;
$realnum++;
if ($magic) {
if (ord($cur) >= 65 && ord($cur) <= 90) {
$blen++;
} else {
$alen++;
}
}
}
}
if ($magic) {
if (($blen * 2 + $alen) == ($len * 2)) break;
if (($blen * 2 + $alen) == ($len * 2) + 1) {
if ($ctype == 1) {
$length -= $cstep;
break;
} else {
break;
}
}
} else {
if ($realnum == $len) break;
}
}
unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep); return substr($title, $start, $length);
}
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);
} $title = "你哈珀niad1纳斯达wop asdni你爱谁都没阿斯顿撒旦12ccs- sd"; $title = utf8Substr($title, 0, 15);
echo $title;

?>

PHP切割汉字的更多相关文章

  1. java按照字节切割字符串,解决汉字的问题

    编写一个截取字符串的函数,输入为一个字符串,截取开始地址,截取字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个, 如“我ABC”,0,4,应该截为“我AB”,输入“我ABC汉DEF”,1, ...

  2. python生成汉字图片字库

    最近做文档识别方面的项目,做汉字识别需要建立字库,在网上找了各种OCR,感觉都不好,这方面的技术应该比较成熟了,OCR的软件很多,但没有找到几篇有含金量量的论文,也没有看到哪位大牛公开字库,我用pyg ...

  3. 黄聪:PHP字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、切割成数组等)

    一.字符串替换 str_replace("iwind", "kiki", "i love iwind, iwind said"); 将输出 ...

  4. C#量转换为汉字表达

    /* 创造者:菜刀打好博客  * 创建日期: 2014年09一个月04号码  * 特征:Money类型转换  *  */ namespace Net.String.ConsoleApplication ...

  5. 【OCR技术系列之二】文字定位与切割

    要做文字识别,第一步要考虑的就是怎么将每一个字符从图片中切割下来,然后才可以送入我们设计好的模型进行字符识别.现在就以下面这张图片为例,说一说最一般的字符切割的步骤是哪些. 当然,我们实际上要识别的图 ...

  6. 【OCR技术系列之四】基于深度学习的文字识别(3755个汉字)

    上一篇提到文字数据集的合成,现在我们手头上已经得到了3755个汉字(一级字库)的印刷体图像数据集,我们可以利用它们进行接下来的3755个汉字的识别系统的搭建.用深度学习做文字识别,用的网络当然是CNN ...

  7. OCR技术浅探 : 文字定位和文本切割(2)

    文字定位 经过前面的特征提取,我们已经较好地提取了图像的文本特征,下面进行文字定位. 主要过程分两步: 1.邻近搜索,目的是圈出单行文字: 2.文本切割,目的是将单行文本切割为单字. 邻近搜索 我们可 ...

  8. 光学字符识别OCR-5 文本切割

    经过前面文字定位得到单行的文本区域之后,我们就可以想办法将单行的文本切割为单个的字符了.因为第三步的模型是针对单个的字符建立的,因此这一步也是必须的. 均匀切割 基于方块汉字的假设,事实上最简单的切割 ...

  9. 【OCR技术系列之二】文字定位于切割

    要做文字识别,第一步要考虑的就是怎么将每一个字符从图片中切割下来,然后才可以送入我们设计好的模型进行字符识别.现在就以下面这张图片为例,说一说最一般的字符切割的步骤是哪些. 当然,我们实际上要识别的图 ...

随机推荐

  1. Vue路由实现之通过URL中的hash(#号)来实现不同页面之间的切换(图表展示、案例分析、附源码详解)

    前言 本篇随笔主要写了Vue框架中路由的基本概念.路由对象属性.vue-router插件的基本使用效果展示.案例分析.原理图解.附源码地址获取. 作为自己对Vue路由进行页面跳转效果知识的总结与笔记. ...

  2. Linux系统执行命令方法

    现在我们无论是工作中还是学习中很多情况下用到Linux系统,当我们需要在C#代码中调用类似与cmd窗口执行命令时候,就需要用到此方法 public static Process CommitComma ...

  3. sqlserver 插入 更新 删除 语句中的 output子句

    官方文档镇楼: https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/ms177564(v=sql.100) 从 ...

  4. 第06组Alpha冲刺(3/6)

    目录 1.1 基本情况 1.2 冲刺概况汇报 1.郝雷明 2.鲍凌函 3.曾丽莉 4. 曹兰英 5. 方梓涵 6.董翔云 7.杜筱 8.黄少丹 9. 詹鑫冰 10.吴沅静 1.3 冲刺成果展示 1.1 ...

  5. uniapp使用scroll-view与swiper组件实现tab滑动切换页面需要注意的问题

    效果图: tab栏可以滑动,切换页面跟随tab栏同步滑动.这里需要注意的是使用swiper组件时,它会有一个默认的高度,你必须动态的获取数据列表的高度覆盖原来的默认高度. 下面是代码 html < ...

  6. SpringBoot 整合文件上传 elment Ui 上传组件

    SpringBoot 整合文件上传 elment Ui 上传组件 本文章记录 自己学习使用 侵权必删! 前端代码 博主最近在学 elment Ui 所以 前端使用 elmentUi 的 upload ...

  7. 在docker中打开redis 客户端 cli

    首先交互方式进入redis容器 docker exec -it redis /bin/bash 随后运行客户端 redis-cli

  8. Neural Networks

    神经网络能够使用torch.nn包构建神经网络. 现在你已经对autogard有了初步的了解,nn基于autograd来定义模型并进行微分.一个nn.Module包含层,和一个forward(inpu ...

  9. 事务@Transactional注解的属性

    事务的传播行为 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己的事务中运行.事务的传播行为可以由传播属性指定.Sprin ...

  10. DIY蓝牙hub F1方向盘

    介于已经入手了自己的第一套OWS FFB直驱力反馈方向盘,因此决定要自己做一款F1方向盘盘面,来提升一下F1 2020游戏的游戏体验感.整体的技术路线如下: [技术路线] 主控:esp32 软件环境: ...