


php smarty Chinese interception plug-in development example_PHP tutorial
Smarty is undoubtedly the most popular and famous template engine in PHP development. By using this template engine, it brings great convenience to our development work. Let’s share the smarty plug-in technology (taking creating a php smarty Chinese string interception as an example), making full use of the various features of smarty, making php smarty a sharper tool in our hands, making our work faster and more efficient.
(1) First we need to know some knowledge about smarty and its plug-ins
1. What is smarty?
Smarty is a template PHP template engine written in PHP. It is a template system recommended by php.net.
2. What is smarty plug-in?
Smarty plug-ins refer to the plugins in smarty, which are some functional control statements embedded in the template. The Variable Modifiers (variable adjustment) in smarty are actually some built-in plug-ins.
3. How does the plug-in work?
In the smarty template, the plug-in is loaded dynamically when using the calling statement. You can put the plug-ins you have written into the plugins directory under the lib directory in the smarty directory, so that when these plug-ins are used in the template, they will be loaded dynamically. will be loaded automatically.
4. How many types of plug-ins are there?
The types of smarty plug-ins are: function, modifier, block, compiler, prefilter, postfilter, outputfilter, resource, insert. In this article we only share how to develop function type plug-ins. The development methods of other types are similar. You can Try to imitate.
5. How to name the plug-in?
File name format:
type.name.php
Type refers to the type, and the ones mentioned above are its selection range;
name: Customized plug-in name, showNews is used in this article to name it;
Function name:
smarty_type_name() smarty: fixed name at a fixed position; type is consistent with the type of the file name, name is consistent with the name in the file name
(2)Now that the basic knowledge is understood, let’s start development. Copy the following code into a file, name it modifier.truncate_cn.php file, and then copy the file to the smarty/lib/plugins/ directory (note that the directory format is not fixed, individuals can choose it according to their own circumstances, but it must be placed in the plugins directory).
/* *作者: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)You can use this plug-in below. In the future, you can directly use the truncate_cn function in the template to intercept Chinese strings, such as: {$news.content|truncate_cn:' 30'}
At this point, a plug-in for the php smarty template is completed. Isn’t it very simple? I hope you can learn and build your own smarty into a more personalized template engine.
Articles you may be interested in
- Using php functions in smarty templates and how to use multiple functions for one variable in smarty templates
- For loop in smarty templates Extension plug-in
- How to clear html format and remove spaces in text and then intercept the text
- PHP method to get file extension (suffix name)
- Smarty temporary file creation Solution to failure
- Use PHP function memory_get_usage to obtain the current PHP memory consumption to optimize program performance
- jquery pop-up window plug-in (compatible with all browsers) share
- linux Detailed explanation of chmod (file or folder permission setting) command parameters and usage

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

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

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.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
