首頁  >  文章  >  後端開發  >  如何在cakephp中截取中文字串?

如何在cakephp中截取中文字串?

黄舟
黄舟原創
2016-12-20 09:28:12971瀏覽

原本以為要自己寫helper去截取中文字串,沒想到cakephp的Text helper類別已經提供了truncate方法。下面是truncate方法的原型。

引用

tru​​ncate 
    truncate(string $text, int $length=100, array $options) 
. th. If 'exact' is passed as false, the truncation will occur after the next word ending. If 'html' is passed as true, html tags will be respected and will not be cut off.


3個參數: 


$text:需要截取的字串

$length:需要截取的長度,預設是截取100個字元

$options:陣列參數。 ending表示在截取後的字串結尾加上ending字串;exact如果是false的話則不會截斷word;html如果是true的話則不會截斷html tag

下面的程式碼示範如何使用tuncate方法:

Php代

echo $this->Text->truncate(   
    'The killer crept forward and tripped on the rug.',   
    22,   
    array(   
        'ending' => '...',   
        'exact' => false   
    )   
);  
echo $this->Text->truncate(
    'The killer crept forward and tripped on the rug.',
    22,
    array(
        'ending' => '...',
        'exact' => false
    )
);

上面的程式碼將輸出The killer crept… 

注意:如果是截取中文字串的話exact最好置為true,否則漢字將無法被截斷
 以上就是如何在cakephp中截斷字串的內容,更多相關內容請關注PHP中文網(www.php.cn)! 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn