


This article mainly introduces the solution to the problem of garbled interception of Chinese and English multi-coded characters in smarty. It involves the modification of the original smartTruncate, which is of great practical value. Friends in need can refer to it
This article explains the examples I have found a solution to the problem of intercepting garbled characters in Chinese and English multi-encoded characters using Smarty, and I would like to share it with you for your reference. The specific method is as follows:
The display of general website pages will inevitably involve the interception of substrings. At this time, truncate comes in handy, but it is only suitable for English users. For Chinese users, Using truncate will cause garbled characters, and for mixed Chinese and English strings, if the same number of strings are intercepted, the actual display lengths will be different, which will appear uneven visually and affect the appearance. This is because the length of one Chinese character is roughly equivalent to the length of two English characters. In addition, truncate is not compatible with GB2312, UTF-8 and other encodings at the same time.
Improved smartTruncate: File name: modifier.smartTruncate.php
The specific code is as follows:
<?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 < strlen($string); $i += $bytes) { $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 < 0) { $start = max(smartStrlen($string) + $start, 0); } for($i = 0; $i < strlen($string); $i += $bytes) { if($start <= 0) { 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 < strlen($string); $j += $bytes) { if($length <= 0) { break; } if(($bytes = ord(substr($string, $j, 1)) > 127 ? $number : 1) > 1) { if($length < 1.0) { 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; } } ?>
The above code fully realizes the original function of truncate, and can It is compatible with GB2312 and UTF-8 encoding at the same time. When judging the character length, a Chinese character is counted as 1.0 and an English character is counted as 0.5, so there will be no uneven situation when intercepting substrings.
Plug-in There is nothing special about the usage. Here is a simple test:
The code is as follows:
{$content|smartTruncate:5:".."}($content等于"A中B华C人D民E共F和G国H")
Display: A Chinese B Chinese C... (Chinese symbol length calculation 1.0, the English symbol length is counted as 0.5, and the length of the omitted symbol is considered)
Whether you use GB2312 encoding or UTF-8 encoding, you will find that the results are correct, which is why I added the word smart in the plug-in name. one.

如何使用自动更正在 Word 中键入箭头在 Word 中键入箭头的最快方法之一是使用预定义的自动更正快捷方式。如果您键入特定的字符序列,Word 会自动将这些字符转换为箭头符号。您可以使用此方法绘制多种不同的箭头样式。要使用自动更正在 Word 中键入箭头:将光标移动到文档中要显示箭头的位置。键入以下字符组合之一:如果您不希望将您键入的内容更正为箭头符号,请按键盘上的退格键会将

上标是一个字符或多个字符,可以是字母或数字,您需要将其设置为略高于正常文本行。例如,如果您需要写1st,则字母st需要略高于字符1。同样,下标是一组字符或单个字符,需要设置为略低于正常文本级别。例如,当你写化学式时,你需要把数字放在正常字符行的下方。以下屏幕截图显示了上标和下标格式的一些示例。尽管这似乎是一项艰巨的任务,但实际上将上标和下标格式应用于您的文本非常简单。在本文中,我们将通过一些简单的步骤说明如何轻松地使用上标或下标格式设置文本。希望你喜欢阅读这篇文章。如何在 Excel 中应用上标

您的物理或数字键盘在表面上提供有限数量的字符选项。但是,有几种方法可以在iPhone、iPad和Mac上访问重音字母、特殊字符等。标准iOS键盘可让您快速访问大写和小写字母、标准数字、标点符号和字符。当然,还有很多其他角色。您可以从带有变音符号的字母到倒置的问号中进行选择。您可能无意中发现了隐藏的特殊字符。如果没有,以下是在iPhone、iPad和Mac上访问它们的方法。如何在iPhone和iPad上访问扩展字符在iPhone或iPad上获取扩展字符非常简单。在“信息”、“

使用Java的Character.isDigit()函数判断字符是否为数字字符在计算机内部以ASCII码的形式表示,每个字符都有一个对应的ASCII码。其中,数字字符0到9分别对应的ASCII码值为48到57。要判断一个字符是否为数字,可以使用Java中的Character类提供的isDigit()方法进行判断。isDigit()方法是Character类的

在matplotlib中正确地显示中文字符,是很多中文用户常常遇到的问题。默认情况下,matplotlib使用的是英文字体,无法正确显示中文字符。为了解决这个问题,我们需要设置正确的中文字体,并将其应用到matplotlib中。下面是一些具体的代码示例,帮助你正确地在matplotlib中显示中文字符。首先,我们需要导入需要的库:importmatplot

如何使用Golang判断一个字符是否为字母在Golang中,判断一个字符是否为字母可以通过使用Unicode包中的IsLetter函数来实现。IsLetter函数会检查给定的字符是否是一个字母。接下来,我们将详细介绍如何使用Golang编写代码来判断一个字符是否为字母。首先,你需要创建一个新的Go文件,用于编写代码。你可以将文件命名为"main.go"。代码

Java中回车键的字符表示是`。在Java中,`表示换行符,当遇到这个字符时,文本输出会换行。下面是一个简单的代码示例,演示如何使用``来表示回车键:publicclassMain{publicstaticvoidmain(String[]args){System.out.println("这是第一行这

Go语言字符串截取方法详解在Go语言中,字符串是不可变的字节序列,因此在进行字符串截取时需要使用一些方法来实现。字符串截取是获取字符串中的特定部分的一种常见操作,可以根据需求截取字符串的前几个字符、后几个字符或者从特定位置截取一定长度的字符。本文将详细介绍在Go语言中如何进行字符串截取,并提供具体的代码示例。使用切片实现字符串截取在Go语言中,可以使用切片来


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
