一般网站页面的显示都不可避免的会涉及子字符串的截取,这个时候truncate就派上用场了,但是它只适合英文用户,对与中文用户来说,使用 truncate会出现乱码,而且对于中文英文混合串来说,截取同样个数的字符串,实际显示长度上却不同,视觉上会显得参差不齐,影像美观。这是因为一个中文的长度大致相当与两个英文的长度。此外,truncate也不能同时兼容GB2312, UTF-8等编码。
改良的smartTruncate: 文件名:modifier.smartTruncate.php
复制代码 代码如下:
function smartDetectUTF8($string)
{
static $result = array();
if(! array_key_exists($key = md5($string), $result))
{
$utf8 = "
/^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)+$/xs
";
$result[$key] = preg_match(trim($utf8), $string);
}
return $result[$key];
}
function smartStrlen($string)
{
$result = 0;
$number = smartDetectUTF8($string) ? 3 : 2;
for($i = 0; $i {
$bytes = ord(substr($string, $i, 1)) > 127 ? $number : 1;
$result += $bytes > 1 ? 1.0 : 0.5;
}
return $result;
}
function smartSubstr($string, $start, $length = null)
{
$result = '';
$number = smartDetectUTF8($string) ? 3 : 2;
if($start {
$start = max(smartStrlen($string) + $start, 0);
}
for($i = 0; $i {
if($start {
break;
}
$bytes = ord(substr($string, $i, 1)) > 127 ? $number : 1;
$start -= $bytes > 1 ? 1.0 : 0.5;
}
if(is_null($length))
{
$result = substr($string, $i);
}
else
{
for($j = $i; $j {
if($length {
break;
}
if(($bytes = ord(substr($string, $j, 1)) > 127 ? $number : 1) > 1)
{
if($length {
break;
}
$result .= substr($string, $j, $bytes);
$length -= 1.0;
}
else
{
$result .= substr($string, $j, 1);
$length -= 0.5;
}
}
}
return $result;
}
function smarty_modifier_smartTruncate($string, $length = 80, $etc = '...',
$break_words = false, $middle = false)
{
if ($length == 0)
return '';
if (smartStrlen($string) > $length) {
$length -= smartStrlen($etc);
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/', '', smartSubstr($string, 0, $length+1));
}
if(!$middle) {
return smartSubstr($string, 0, $length).$etc;
} else {
return smartSubstr($string, 0, $length/2) . $etc . smartSubstr($string, -$length/2);
}
} else {
return $string;
}
}
?>
以上代码完整实现了truncate的原有功能,而且可以同时兼容GB2312和UTF-8编码,在判断字符长度的时候,一个中文字符算1.0,一个英文字符算0.5,所以在截取子字符串的时候不会出现参差不齐的情况.
插件的使用方式没有特别之处,这里简单测试一下:
{$content|smartTruncate:5:".."}($content等于"A中B华C人D民E共F和G国H")
显示:A中B华C.. (中文符号长度算1.0,英文符号长度算0.5,并且考虑省略符号的长度)
不管你是使用GB2312编码还是UTF-8编码,你会发现结果都正确,这也是为什么我在插件名字里加上smart字样的原因之一。

php导入csv乱码问题的解决办法:1、构造一个解析函数“function tb_str_getcsv($string, $delimiter=',', $enclosure='"') {...}”;2、读取文件到变量;3、通过“substr($s,2)”去掉BOM头即可。

cmd php乱码的解决办法:1、在windows平台的cmd或shellpower中,执行php脚本并检查是否乱码;2、通过在php脚本里执行“exec("CHCP 65001");”命令去修改当前页码字符集即可。

随着时代的发展,我们越来越注重对数据的校验,特别是对用户输入的校验。对于语言类的校验,如何准确判定输入是否全部为中文字符成为了一个重要问题。而在golang中,我们可以借助unicode包和regexp包来实现这一需求。一、unicode包unicode包提供了一系列对于unicode的核心支持。我们可以使用这个包中的函数来准确地判断一个字符是否为中文字符。

如何在C语言编程软件中实现中文字符排序功能?在现代社会,中文字符排序功能在很多软件中都是必不可少的功能之一。无论是在文字处理软件、搜索引擎还是数据库系统中,都需要对中文字符进行排序,以便更好地展示和处理中文文本数据。而在C语言编程中,如何实现中文字符排序功能呢?下面将简要介绍一种方法。首先,为了在C语言中实现中文字符排序功能,我们需要使用到字符串比较函数。然

如何在PHP中处理中文字符的拼音排序问题?在开发中文网站或应用时,经常会面临需要对中文字符串按照拼音进行排序的需求。然而,由于中文字符的复杂性,直接使用常规的排序算法会导致排序结果出现错误。因此,我们需要使用一种特殊的方法来处理中文字符的拼音排序问题。在PHP中,有一种常用的解决方案是使用拼音库,如“Overtrue/Pinyin”。这是一个基于PHP的拼音

php cp936乱码的解决办法:1、打开相应的PHP文件;2、查找“mb_convert_encoding($str, 'UTF-8', 'CP936');”代码;3、使用“iconv('utf-8', 'latin1//IGNORE', $str);”方法进行转码即可。

在PHP中,正则表达式是一种常用的字符串匹配工具,它可以用来判断一个字符串是否符合某种特定的格式,从而实现对输入值的有效性验证。而在处理中文字符时,由于中文字符与英文字符在编码方式上有所不同,因此需要相应地调整正则表达式的匹配规则。本文将介绍如何在PHP中使用正则表达式来匹配中文字符。一、了解中文字符编码PHP中常用的字符编码有UTF-8和G

PHP如何获取中文字符的首字母?在处理中文字符时,有时候我们需要获取中文字符的首字母。PHP提供了一些内置函数和扩展包来实现这一功能。一种常用的方式是使用mb_substr()函数结合ord()函数。mb_substr()函数用于获取字符串的子串,ord()函数用于获取字符的ASCII码值。我们可以通过获取中文字符的第一个字符及其ASCII码值来获取首字母。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
