php smarty中文截取插件开发示例
smarty 无疑是 php 开发里面目前最流行最出名的模板引擎了,通过使用该模板引擎,给我们的开发工作中带来了极大的方便。下面分享一下 smarty 插件技术(以创建一个 php smarty 中文字符串截取为例),充分利用 smarty 的各种特性,使 php smarty 成为我们手中更为锋利的利器,让我们的工作更加快速高效。
(1) 首先我们需要了解一下 smarty 及其插件的一些知识
1. 什么是smarty?
smarty是一个使用PHP写出来的模板PHP模板引擎, 是php.net推荐的一个模板系统.
2. 什么是smarty的插件?
smarty的插件是指smarty中的plugins, 是一些嵌入模板内的一些功能性控制语句, smarty中的Variable Modifiers(变量调节)实际就是一些内置的插件。
3. 插件是怎么工作的?
在smarty模板中使用了插件调用语句时动态的载入, 你可以将你写好的插件放入smarty目录中的lib目录下的plugins目录里面, 这样在模板中使用这些插件时它将会被自动载入。
4. 插件有几种类型?
smarty 插件的类型有:function, modifier, block, compiler,prefilter, postfilter, outputfilter, resource, insert,本篇文章我们只分享一下如何开发 function 类型的插件,其它类型的开发方法大同小异,大家可以模仿试试。
5. 如何命名插件?
文件名形式:
type.name.php
type指的是类型,上边提到的几种就是它的选择范围;
name: 自定义的插件名称,本文中使用showNews来命名;
函数名称:
smarty_type_name() smarty:固定位置的固定名称; type与文件名的type一致, name与文件名中的name一致
(2)基础知识明白了,下面就开始开发了。将以下代码拷贝到文件中,命名为 modifier.truncate_cn.php 文件,然后将该文件拷贝到 smarty/lib/plugins/ 目录下面(注意这个目录形式不是固定的,个人根据自己的情况来,但必定是放在plugins目录里面)。
/* *作者:http://www.phpernote.com/ *时间:2013年1月31日06:31:52 *作用:截取中文字符串 */ function smarty_modifier_truncate_cn($string,$length=0,$ellipsis='…',$start=0){ $string=strip_tags($string); $string=preg_replace('/\n/is','',$string); //$string=preg_replace('/ | /is','',$string);//清除字符串中的空格 $string=preg_replace('/ /is','',$string); 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]/",$string,$string); if(is_array($string)&&!empty($string[0])){ $string=implode('',$string[0]); if(strlen($string)<$start+1){ return ''; } preg_match_all("/./su",$string,$ar); $string2=''; $tstr=''; //www.phpernote.com for($i=0;isset($ar[0][$i]);$i++){ if(strlen($tstr)<$start){ $tstr.=$ar[0][$i]; }else{ if(strlen($string2)<$length+strlen($ar[0][$i])){ $string2.=$ar[0][$i]; }else{ break; } } } return $string==$string2?$string2:$string2.$ellipsis; }else{ $string=''; } return $string; }
(3)下面就可以使用该插件了,以后在模板里面就可以直接使用 truncate_cn 函数来进行中文字符串的截取了,比如:{$news.content|truncate_cn:'30'}
至此,php smarty 模板的一个插件就这么完成了,是不是非常简单,希望你能学会并将自己的 smarty 打造成一个更为个性的模板引擎。
您可能感兴趣的文章
- smarty模板中使用php函数以及smarty模板中如何对一个变量使用多个函数
- smarty模板中for循环的扩展插件
- php如何清除html格式并去除文字中的空格然后截取文字
- PHP 获取文件扩展名(后缀名)的方法
- Smarty临时文件创建失败的解决办法
- 用PHP函数memory_get_usage获取当前PHP内存消耗量以实现程序的性能优化
- jquery弹出窗口插件(兼容所有浏览器)分享
- linux chmod(文件或文件夹权限设定)命令参数及用法详解

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

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

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
