Development example of the smarty Chinese interception plug-in

Source: Internet
Author: User
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. The following describes the smarty plug-in technology (taking the creation of a phpsmarty Chinese string truncation as an example)

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. Next, let's share the smarty plug-in technology (taking the creation of a php smarty Chinese string truncation as an example), and make full use of the various features of smarty to make php smarty a sharp weapon in our hands, this makes 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 PHP template engine written in PHP and a template system recommended by php.net.

2. what is the smarty plug-in?

The smarty plug-in refers to the plug-ins in smarty, which are some functional control statements embedded in the template. the Variable Modifiers in smarty is actually some built-in plug-ins.

3. how does the plug-in work?

The smarty template uses the plug-in call statement to dynamically load the file. you can put the plug-in you have written into the plugins directory under the lib directory in the smarty directory, in this way, these plug-ins will be automatically loaded when they are used in the template.

4. what types of plug-ins are available?

Smarty plug-ins are of the following types: function, modifier, block, compiler, prefilter, postfilter, outputfilter, resource, and insert. in this article, we will only share how to develop function plug-ins, other types of development methods are similar, so you can try them.

5. how to name the plug-in?

File name format:

Type. name. php

Type refers to the type. the above mentioned types are its selection range;

Name: Custom plug-in name. this document uses showNews to name it;

Function name:

Smarty_type_name () smarty: a fixed name at a fixed position; type is consistent with the type of the file name, and name is consistent with the name in the file name

(2)Now that you understand the basic knowledge, we will start development. Copy the following code to the file and 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. you can use it based on your own situation, but it must be in the plugins directory ).

 

/** Author: https://www.php1.cn */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); // clear the spaces in the 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); I F (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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.