PHP smarty Function Extension

Source: Internet
Author: User

Chinese: modifier.cn _ truncate. php CopyCode The Code is as follows: function smarty_modifier_cn_truncate ($ string, $ strlen = 20, $ etc = '...',
$ Keep_first_style = false)
{
$ Strlen = $ strlen * 2;
$ String = trim ($ string );
If (strlen ($ string) <= $ strlen ){
Return $ string;
}
$ STR = strip_tags ($ string );
$ J = 0;
For ($ I = 0; $ I <$ strlen; $ I ++ ){
If (ord (substr ($ STR, $ I, 1)> 0xa0) $ J ++;
}
If ($ J % 2! = 0) $ strlen ++;
$ Rstr = substr ($ STR, 0, $ strlen );
If (strlen ($ Str)> $ strlen) {$ rstr. = $ etc ;}
If ($ keep_first_style = true & ereg ('^ <(. *) >$', $ string )){
If (strlen ($ Str) <= $ strlen ){
Return $ string;
}
$ Start_pos = strpos ($ string, substr ($ STR, 0, 4 ));
$ End_pos = strpos ($ string, substr ($ STR,-4 ));
$ End_pos = $ end_pos + 4;
$ Rstr = substr ($ string, 0, $ start_pos). $ rstr. substr ($ string, $ end_pos, strlen ($ string ));
}
Return $ rstr;
}

Modifier. UBB. php
Copy code The Code is as follows: <? PHP
Function smarty_modifier_ubb ($ Str ){
$ Ubbs ['quote'] [0] = '/\ [quote \] (. *?) \ [\/Quote \]/s ';
$ Ubbs ['quote'] [1] = '<BLOCKQUOTE> reference: <HR size = 1> \ 1 <HR size = 1> </BLOCKQUOTE> ';
$ Ubbs ['B'] [0] = '/\ [B \] (. *?) \ [\/B \]/s ';
$ Ubbs ['B'] [1] = '<B> \ 1 </B> ';
$ Ubbs ['I'] [0] = '/\ [I \] (. *?) \ [\/I \]/s ';
$ Ubbs ['I'] [1] = '<I> \ 1 </I> ';
$ Ubbs ['U'] [0] = '/\ [U \] (. *?) \ [\/U \]/s ';
$ Ubbs ['U'] [1] = '<u> \ 1 </u> ';
$ Ubbs ['lil'] [0] = '/\ [Li \] (. *?) \ [\/Li \]/s ';
$ Ubbs ['lil'] [1] = '<li> \ 1 </LI> ';
$ Ubbs ['img '] [0] ='/\ [img \] (. *?) \ [\/IMG \]/s ';
$ Ubbs ['img '] [1] =' ';
$ Ubbs ['img2'] [0] = '/\ [img = ([^,] +), ([^ \] +) \] (. *?) \ [\/IMG \]/s ';
$ Ubbs ['img2'] [1] = ' ';
$ Ubbs ['url'] [0] = '/\ [url \] (. *?) \ [\/URL \]/s ';
$ Ubbs ['url'] [1] = '<a href = "\ 1" target = "_ blank"> \ 1 </a> ';
$ Ubbs ['url2 '] [0] ='/\ [url = ([^ \] +) \] (. *?) \ [\/URL \]/s ';
$ Ubbs ['url2 '] [1] =' <a href = "\ 1" target = "_ blank"> \ 2 </a> ';
$ Ubbs ['email '] [0] ='/\ [email = ([^ \] +) \] (. *?) \ [\/Email \]/s ';
$ Ubbs ['email '] [1] =' <a href = "mailto: \ 2"> \ 2 </a> ';
$ Ubbs ['qq'] [0] = '/\ [QQ \] ([0-9] {5, 10}) \ [\/QQ \]/s ';
$ Ubbs ['qq'] [1] = '<a target = "new" href = "Tencent: // message /? Uin = \ 1 & Site = www.52515.net & menu = yes "> </a> ';
$ Ubbs ['color'] [0] = '/\ [color = ([^ \] +) \] (. *?) \ [\/Color \]/s ';
$ Ubbs ['color'] [1] = '<font color = "\ 1"> \ 2 ';
$ Ubbs ['font'] [0] = '/\ [font = ([^ \] +) \] (. *?) \ [\/Font \]/s ';
$ Ubbs ['font'] [1] = '<font face = "\ 1"> \ 2 ';
$ Ubbs ['SIZE'] [0] = '/\ [size = ([0-9] +) \] (. *?) \ [\/Size \]/s ';
$ Ubbs ['SIZE'] [1] = '<font size = "\ 1" style = "line-Height: \ 1em;"> \ 2 ';
$ Ubbs ['sizept '] [0] ='/\ [size = ([0-9, pt] +) \] (. *?) \ [\/Size \]/s ';
$ Ubbs ['sizept '] [1] =' <font style = "font-size: \ 1"> \ 2 ';
$ Ubbs ['align '] [0] ='/\ [align = ([^ \] +) \] (. *?) \ [\/Align \]/s ';
$ Ubbs ['align '] [1] =' <Div align = "\ 1"> \ 2 ';
$ Ubbs ['media'] [0] = '/\ [media \] (. *?) \ [\/Media \]/s ';
$ Ubbs ['media'] [1] = '<embed src = "\ 1"> </embed> ';
$ Ubbs ['LIST'] [0] = '/\ [list \] (. *?) \ [\/List \]/s ';
$ Ubbs ['LIST'] [1] = '<ul> \ 1 </ul> ';
$ Ubbs ['list2'] [0] = '/\ [list = ([0-9] +) \] (. *?) \ [\/List \]/s ';
$ Ubbs ['list2'] [1] = '<ol> \ 2 </OL> ';
$ Ubbs ['indent '] [0] ='/\ [indent \] (. *?) \ [\/Indent \]/s ';
$ Ubbs ['indent '] [1] =' <Div style = "text-indent: 20px;"> \ 1 ';
$ Ubbs ['table'] [0] = '/\ [Table ([=, 0-9, %] *) \] (. *?) \ [\/Table \]/s ';
$ Ubbs ['table'] [1] = '<Table Style = "border: # 0099ff 1px dotted; border-right-width: 0px; border-top-width: 0px; "> \ 2 ';
$ Ubbs ['tr'] [0] = '/\ [tr \] (. *?) \ [\/Tr \]/s ';
$ Ubbs ['tr'] [1] = '<tr> \ 1 ';
$ Ubbs ['td '] [0] ='/\ [TD \] (. *?) \ [\/TD \]/s ';
$ Ubbs ['td '] [1] =' <TD style = "border: # 0099ff 1px dotted; border-left-width: 0px; border-bottom-width: 0px; "> \ 1 ';
$ Ubbs ['td2 '] [0] ='/\ [TD ([^ (\] | \ [)] +) \] (. *?) \ [\/TD \]/s ';
$ Ubbs ['td2 '] [1] =' <TD style = "border: # 0099ff 1px dotted; border-left-width: 0px; border-bottom-width: 0px; "> \ 2 ';
$ STR = str_replace ('[*]', '<li>', $ Str );
Foreach ($ ubbs as $ UBB ){
$ STR = ubb_replace ($ UBB [0], $ UBB [1], $ Str );
}
Return $ STR;
}
Function ubb_replace ($ pattern, $ replacement, $ Str ){
Do {
$ Str_ret = $ STR;
$ STR = preg_replace ($ pattern, $ replacement, $ Str );
} While ($ Str );
Return $ str_ret;
}
?>

code modifier. unescape. php copy Code the code is as follows: function smarty_modifier_unescape ($ Str)
{< BR >$ STR = rawurldecode ($ Str );
preg_match_all ("/(?: % U. {4}) | [^ %] +/", $ STR, $ R);
$ AR = $ R [0];
foreach ($ Ar as $ k => $ v) {
If (substr ($ V, 0, 2) = "% u" & strlen ($ v) = 6)
$ ar [$ K] = iconv ("UCS-2", "gb2312 ", @ pack ("H4", substr ($ V,-4);
}< br> return join ("", $ AR );
}

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.