首页 > 开发 > PHP > 正文

cakephp中截取中文字符串

2024-05-04 21:58:21
字体:
来源:转载
供稿:网友

Text->truncate方法有3个参数:

$text:需要截取的字符串 

$length:需要截取的长度,默认是截取100个字符 

$options:数组参数,ending表示在截取后的字符串结尾加上ending字符串,exact如果是false的话则不会截断word,html如果是true的话则不会截断html tag.

下面的代码演示了如何使用tuncate方法:

  1. echo $this->Text->truncate( 
  2.     'The killer crept forward and tripped on the rug.', 22,  
  3.     array(  
  4.         'ending' => '...',  
  5.         'exact' => false  
  6.     )  
  7. ); 
  8. //上面的代码将输出The killer crept… 

注意:如果是截取中文字符串的话exact最好置为true,否则汉语字符将无法被截断.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表