php smarty truncate UTF8乱码问题解决办法
估计不少玩smarty模板的小朋友都遇到过裁切乱码问题。特别是UTF8编码的。
以下代码保存为modifier.truncate2.php 存到smarty libs下的plugin目录下
然后裁剪的时候用$v->content|truncate2:100
就搞定了。
如果不好用可能是缓存导致,请速度删除templates_c下的缓存文件(小编搞的时候遇到缓存问题。)
复制代码 代码如下:
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty truncate modifier plugin
*
* Type: modifier
* Name: truncate
* Purpose: Truncate a string to a certain length if necessary,
* optionally splitting in the middle of a word, and
* appending the $etc string or inserting $etc into the middle.
* @link http://smarty.php.net/manual/en/language.modifier.truncate.php
* truncate (Smarty online manual)
* @author Monte Ohrt
* @param string
* @param integer
* @param string
* @param boolean
* @param boolean
* @return string
*/
function smarty_modifier_truncate2( $string,$length = 80,$etc='...',$count_words = true ) {
return $returnstr =substr_utf8($string, 0, $length).$etc;
}
function substr_utf8($str, $start=0, $length=-1, $return_ary=false) {
$len = strlen($str);if ($length == -1) $length = $len;
$r = array();
$n = 0;
$m = 0;
for($i = 0; $i $x = substr($str, $i, 1);
$a = base_convert(ord($x), 10, 2);
$a = substr('00000000'.$a, -8);
if ($n if (substr($a, 0, 1) == 0) {
}elseif (substr($a, 0, 3) == 110) {
$i += 1;
}elseif (substr($a, 0, 4) == 1110) {
$i += 2;
}
$n++;
}else {
if (substr($a, 0, 1) == 0) {
$r[] = substr($str, $i, 1);
}elseif (substr($a, 0, 3) == 110) {
$r[] = substr($str, $i, 2);
$i += 1;
}elseif (substr($a, 0, 4) == 1110) {
$r[] = substr($str, $i, 3);
$i += 2;
}else {
$r[] = '';
}
if (++$m >= $length) {
break;
}
}
}
return $return_ary ? $r : implode("",$r);
}
/* vim: set expandtab: */
?>
samrty的插件体系还是比较智能而且容易修改的。

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
