Questions about calling PHP built-in functions in smarty

WBOY
Release: 2016-07-25 09:05:42
Original
863 people have browsed it
  1. function SmartyLen($params){
  2. extract($params);
  3. $len=strlen($text);
  4. $max=$length;
  5. for ($i=0;$ i<$length;$i++){
  6. $chr=substr($text,$i,1);
  7. if(ord($chr)>0x80)//The character is Chinese
  8. {
  9. $length++;
  10. $i++ ;
  11. $len--;
  12. }
  13. }
  14. $str=substr($text,0,$length);
  15. if($len>$max)$str.="...";
  16. Return $str;
  17. }
Copy code

Register function $smarty->register_function('len',"SmartyLen"); Template call {len text="under test" length="1"} //Note that text and length here are actually two parameters in the function. The parameters of the function must be the same as the internal variable name of the function. In fact, they are passed back is an array, use extract($params); in the function to import the variables in the array into the current symbol table.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template