PHP smarty Chinese intercept plugin development Sample _php Tutorial

Source: Internet
Author: User
Tags php template smarty template
Smarty is undoubtedly the most popular and most famous template engine in PHP development, which brings great convenience to our development work by using the template engine. Share the following Smarty plug-in technology (to create a PHP smarty Chinese string interception as an example), take full advantage of the various features of smarty, so that PHP smarty become a sharper tool in our hands, so that our work more quickly and efficiently.

(1) first we need to understand some of the knowledge of Smarty and its plugins

1. What is Smarty?

Smarty is a template php template engine written in PHP, is a template system recommended by Php.net.

2. What is the plugin for Smarty?

Smarty plug-in refers to the smarty in the plugins, is some embedded template in some functional control statements, Smarty variable Modifiers (variable adjustment) is actually some built-in plug-ins.

3. How does the plugin work?

In the Smarty template using the plug-in call statement when the dynamic loading, you can put your written plug-in into the Smarty directory in the plugins directory of the Lib directory, so that in the template use these plugins will be automatically loaded.

4. Are there several types of plugins?

The types of Smarty plugins are: function, modifier, block, Compiler,prefilter, Postfilter, Outputfilter, resource, INSERT, This article we only share how to develop the function type of plug-ins, other types of development methods are similar, we can imitate try.

5. How do I name plugins?

File Name form:

type.name.php

Type refers to the types, the above mentioned are its choice range;

Name: Custom plug-in name, this article uses shownews to name;

Function Name:

Smarty_type_name () Smarty: Fixed name of fixed position; Type is the same as the type of the file name, and name matches the name in the file name

(2) The basic knowledge is understood, the following begins to develop. Copy the following code to the file, name the modifier.truncate_cn.php file, and then copy the file to the smarty/lib/plugins/directory (note that this directory form is not fixed, the individual according to their own situation, But must be placed in the plugins directory).

/** Author: http://www.phpernote.com/* time: January 31, 2013 06:31:52* function: Intercept Chinese string */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 space in the string $string=preg_replace ('//is ', ' ', $string);p reg_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), $string 2= "; $tstr =";//www.phpernote.comfor ($i =0;isset ($ar [0][$i]); $i + +) { if (strlen ($TSTR) < $start) {$tstr. = $ar [0][$i];} Else{if (strlen ($string 2) < $length +strlen ($ar [0][$i]) {$string 2.= $ar [0][$i];} Else{break;}}} return $string = = $string 2? $string 2: $string 2. $ellipsis;} else{$string = ';} return $string;}

(3) the plug-in can be used below, in the template can be directly used in the TRUNCATE_CN function to intercept the Chinese string, for example: {$news. CONTENT|TRUNCATE_CN: ' 30 '}

At this point, php smarty template of a plug-in is so complete, is not very simple, I hope you can learn and make your own smarty to create a more personalized template engine.

Articles you may be interested in

    • Use PHP functions in Smarty Templates and how to use multiple functions for a variable in a smarty template
    • Smarty extension for loop in template
    • How PHP clears HTML formatting and removes whitespace from text and then intercepts text
    • How PHP Gets the file extension (suffix name)
    • Smarty Workaround for temporary file creation failure
    • Use PHP function memory_get_usage to get the current PHP memory consumption to achieve program performance optimization
    • jquery Popup Plugin (compatible with all browsers) share
    • Linux chmod (file or folder permission settings) command parameters and usage details

http://www.bkjia.com/PHPjc/764122.html www.bkjia.com true http://www.bkjia.com/PHPjc/764122.html techarticle Smarty is undoubtedly the most popular and most famous template engine in PHP development, which brings great convenience to our development work by using the template engine. Share the following ...

  • 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.