php汉字截取
/**
* 截取HTML,并自动补全闭合
* @param $html
* @param $length
* @param $end
*/
function subHtml($html,$length,$charset) {
$result = '';
$tagStack = array();
$len = 0;
$contents = preg_split("~(<[^>]+?>)~si",$html, -1,PREG_SPLIT_NO_EMPTY| PREG_SPLIT_DELIM_CAPTURE); foreach($contents as $tag)
{ if (trim($tag)=="") continue; if(preg_match("~<([a-z0-9]+)[^/>]*?/>~si",$tag)){
$result .= $tag;
}else if(preg_match("~</([a-z0-9]+)[^/>]*?>~si",$tag,$match)){
if($tagStack[count($tagStack)-1] == $match[1]){
array_pop($tagStack);
$result .= $tag;
}
}else if(preg_match("~<([a-z0-9]+)[^/>]*?>~si",$tag,$match)){
array_push($tagStack,$match[1]);
$result .= $tag;
}else if(preg_match("~<!--.*?-->~si",$tag)){
$result .= $tag;
}else{
if($len + mstrlen($tag, $charset) < $length){
$result .= $tag;
$len += mstrlen($tag, $charset);
}else {
$str = msubstr($tag,0,$length-$len+1,'', $charset);
$result .= $str;
break;
}
}
} while(!empty($tagStack)){
$result .= '</'.array_pop($tagStack).'>';
} return $result;
} /**
* 取得字符串的长度,包括中英文。
*/
function mstrlen($str,$charset = 'UTF-8'){
if (function_exists('mb_substr')) {
$length=mb_strlen($str,$charset);
} elseif (function_exists('iconv_substr')) {
$length=iconv_strlen($str,$charset);
} else {
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]/", $text, $ar);
$length=count($ar[0]);
}
return $length;
} /**
* 截取中文字符串
* @param $string 字符串
* @param $start 起始位
* @param $length 长度
* @param $charset 编码
* @param $dot 附加字串
*/
function msubstr($string, $start, $length,$dot='...',$charset = 'UTF-8') {
$string = str_replace(array('&', '"', '<', '>',' '), array('&', '"', '<', '>',' '), $string);
if(strlen($string) <= $length) {
return $string;
}
if(strtolower($charset) == 'utf-8') {
$n = $tn = $noc = 0;
while($n < strlen($string)) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2;
} elseif(224 <= $t && $t <= 239) {
$tn = 3; $n += 3;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6;
} else {
$n++;
}
$noc++;
if($noc >= $length) {
break;
}
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
} else {
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
return $strcut.$dot;
}
php汉字截取的更多相关文章
- UTF-8、GB2312都支持的汉字截取函数
<?php/*Utf-8.gb2312都支持的汉字截取函数cut_str(字符串, 截取长度, 开始长度, 编码);编码默认为 utf-8开始长度默认为 0*/ function cut_str ...
- PHP中汉字截取
$len = 19; $text = "怎么将新闻的很长的标题只显示前面一些字,后面用.....来代替?"; echo strlen($text)<=$len ? $text ...
- php中文汉字截取函数
public function substrgb($in,$num) { //$num=16; $pos=0; $bytenum=0; $out=""; while($num){ ...
- 从给定字符串中截取n个字节的字符(解决汉字截取乱码问题)
function GetNBytesChar(s: Ansistring; n: Integer): string;var aStr: AnsiString; bStr: WideString;b ...
- ***PHP各种编码的汉字字符串截取
虽然PHP有现成的截取字符串函数substr(),但是这个函数不能对汉字字符串进行截取,要实现这种效果还需要我们自己去编写相应的函数.汉字有多种编码,比如GB2312,UTF-8等,汉字字符串的截取需 ...
- php截取字符串
1.substr(源字符串,其实位置[,长度])-截取字符串返回部分字符串 <?php $str ="phpddt.com"; echo substr($str, 2); / ...
- (实用篇)多个PHP中文字符串截取函数
字符串截取是一个非常常见的编程任务,而往往带中文的字符串截取会经常用到.虽然不难,但是自己写函数实现又耗费时间,这里介绍一个比较好用的字符串截取函数,能够胜任基本的需求了 <?php funct ...
- php字符串截取问题
希望将一个字符串限长显示,如果该字符串超过一定长数,就截取前n个字符,后加省略号. 但是在英文和汉字混合的情况下会出现如下问题: 如果有这样一个字符串 $str="这是一个字符串" ...
- PHP 截取字符串专题
1. 截取GB2312中文字符串 < ?php//截取中文字符串function mysubstr($str, $start, $len) { $tmpstr = "" ...
随机推荐
- android分割线
http://blog.csdn.net/dekunchenivan/article/details/6640804 在Android布局文件layout中设置水平分割线: <View ...
- 彻底解决TAP(点透)提升移动端点击响应速度
使用fastclick 尼玛使用太简单了,直接一句: FastClick.attach(document.body); 于是所有的click响应速度直接提升,刚刚的!什么input获取焦点的问题也解决 ...
- 《C++ 标准库》读书笔记 - 第二章 Introduction to C++ and the Standard Library
1. History of the C++ Standards 1.1 History of the C++ Standards C++98 -> C++03 -> TR1 -> C ...
- 出现No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here异常
问题描述: public void save(BaseEntity baseEntity) { Session session = null; try { session = currentSessi ...
- 如何截取url中的各个参数?
在页面跳的时候,目的界面可能会根据url中的某些参数进行数据处理,这个时候如何能快速并设计一个通用的截取url中的参数,并且获取各个参数值? 代码: url = location.search;//获 ...
- centos 添加epel、remi仓库和ELRepo仓库
centos使用yum安装软件非常方便,yum会自动安装软件的相关依赖.但是centos自带的源仓库,软件相对老旧并且不太全,所以我们可以添加第三方仓库,可以安装较新的软件版本. epel是fedor ...
- 收MUD巫师学徒,MUD开发,LPC语言开发
收MUD巫师学徒,MUD开发,LPC语言开发 对这个有兴趣的联系我,签订协议 Q 184377367
- jQuery学习-事件之绑定事件(六)
在jQuery中,为了屏蔽event对象在各浏览器中的差异性,它使用了自定的Event对象,如下: 1 jQuery.Event = function( src, props ) { 2 ...
- API之IP地址查询---权威的IP地址查询接口集合
原文地址:http://yushine.iteye.com/blog/1717586 推荐实用IP138 http://www.baidu.com/s?wd=IP&rsv_spt=1& ...
- denoising autoencoder
神经网络的挑战和关键技术: 1.神经网络结构决定(层,神经元,连接) 加入特定领域的知识(CNN 图片处理) 2.模型复杂度高 大的数据量: regularization: dro ...