php截取字符串,避免乱码
转载请注明来源:https://www.cnblogs.com/hookjc/
1. 截取GB2312中文字符串
<?php
//截取中文字符串
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;
}
?>
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);
}
?>
3. UTF-8、GB2312都支持的汉字截取函数
<?php
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 = "abcd需要截取的字符串";
echo cut_str($str, 8, 0, 'gb2312');
?>
4. BugFree 的字符截取函数
<?php
function sysSubStr($String,$Length,$Append = false)
{
if (strlen($String) <= $Length )
{
return $String;
}
else
{
$I = 0;
while ($I < $Length)
{
$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 = "CodeBit.cn -- 简单、精彩、通用";
$Length = "18";
$Append = false;
echo sysSubStr($String,$Length,$Append);
?>
来源:python脚本自动迁移
php截取字符串,避免乱码的更多相关文章
- PHP用substr截取字符串出现中文乱码问题用mb_substr
PHP用substr截取字符串出现中文乱码问题用mb_substr实例:mb_substr('截取中文乱码问题测试',0,5, 'utf-8'); 语法 : string substr (string ...
- php实现中文字符串无乱码截取
在PHP开发中会经常用到字符串截取,有的时候字符串截取会出现乱码的情况,那么怎么解决这个问题呢,其实也很容易 首先我们要了解关于中英文占多少字节的问题. ASCII码:一个中文汉字占两个字节的空间. ...
- 解决在C#(.net)按字节数截取字符串最后出现乱码的问题
最近需要用到按字节数截取字符串.在网上找了很多方法. Encoding.Default.GetString采用的DefaultEncoding.UTF8.GetBytes采用的是utf-8编码.这样当 ...
- php截取中文字符串无乱码的方法
利用php内置方法mb_substr截取中文不乱码,使用起来非常简单 <?php $str = '我喜欢laravel or yii2'; echo mb_substr($str, 0, 1, ...
- php截取字符串
1.substr(源字符串,其实位置[,长度])-截取字符串返回部分字符串 <?php $str ="phpddt.com"; echo substr($str, 2); / ...
- 用substr()截取中文出现乱码的解决方法
截取中文字符串时出现乱码(使用substr()函数) 程序一:PHP截取中文字符串方法 function msubstr($str, $start, $len) { $tmpstr = &quo ...
- SQL截取字符串
SUBSTRING 返回字符.binary.text 或 image 表达式的一部分.有关可与该函数一起使用的有效 Microsoft® SQL ...
- sql语句中截取字符串
今天在开发过程中因为要用到合并单元格,在程序里实现了以后,查出来的数据太长,都把格式撑大了,后来想想可以在sql语句查询的时候就截取,就去网上找了一下,挺好用,就转了过来: 合并单元格: /// &l ...
- php截取字符串|php截取字符串前几位|php截取中文字符串
转 截取字符串专题:php截取字符串函数,php 字符串长度,php截取字符串前几位 PHP截取中文字符串(mb_substr)和获取中文 => http://www.q3060.com/lis ...
- 【Go】高效截取字符串的一些思考
原文链接:https://blog.thinkeridea.com/201910/go/efficient_string_truncation.html 最近我在 Go Forum 中发现了 [SOL ...
随机推荐
- CS5265替代LT8711设计TYPEC转HDMI 4K高清投屏方案|LT8711龙迅替代方案
龙迅LT8711是一款Type-C/DP1.2 to HDMI2.0方案芯片.LT8711HE是一款高性能Type-C/DP1.2至HDMI2.0转换器,设计用于将USB typec或DP1.2源连接 ...
- Java_Swing中关于关闭窗口的方法
注意:在继承Jframe的过程中,由于工具的快捷功能可以会继承到Frame类,继承到Jframe类打开的窗口默认是可以关闭的,而Frame类打开的窗口点了关闭的没有反应的. 1.关闭子窗口后,父窗口也 ...
- 最详细的git使用教程
git的安装可以参考我的博客:https://www.cnblogs.com/shanfeng1000/p/10969116.html 一.工作流程 Git 工作流程一般是: 克隆 Git 资源作为工 ...
- 在B站学Java
大家好,我是大彬~ 众所周知,B站是用来搞学习的,对于学编程的小伙伴来说,B站有着非常丰富的学习资源.今天给大家分享一些质量比较高的Java学习视频,希望对大家有帮助! Java基础 首先是Java基 ...
- linux 之 命令提示符设置
linux 命令提示符设置实际就是设置环境变量PS1的值. 参数说明 \d 显示时间,星期 月 日 \h 显示简写主机名,如默认主机名 localhost \t 显示24小时制时间, HH:MM:SS ...
- 初识python: 模块定义及调用
一.定义 模块:用来从逻辑上组织python代码(变量.函数.类.逻辑:实现一个功能),本质就是.py结尾的python文件(比如:文件名:test.py,对应的模块名:test) 包:用来从逻辑上组 ...
- Windows Batch 编程 和 Powershell 编程
Batch Script - Functions with Return Values https://www.tutorialspoint.com/batch_script/batch_script ...
- c# - 实体类和有参无参构造函数的具体写法
1.前言 与Java基本一模一样,但是rider貌似没有意见生成get和set方法的指令 2.操作 (1)目录 实体源码 namespace ConsoleApp1.entity { public c ...
- 怎样从 bat 批处理文件调用 PowerShell 脚本
https://stackoverflow.com/questions/19335004/how-to-run-a-powershell-script-from-a-batch-file https: ...
- LC 二叉树的最大深度
https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnd69e/ Recursion /** * Definitio ...