在我的CI应用程序中,我正在显示数组中的文本:
<?php echo $music[0]['title']?>
限制文字大小的最简单方法是什么?我想要根据字符或像素大小限制文字。我尝试使用CSS但它只适用于实际文本。任何帮助都是合适的。
答案 0 :(得分:1)
//IN CONTROLLER
$this->load->helper('text');
//In view file
//For Word Limiter use word_limiter($str, $limit = 100, $end_char = '…');
//FOr Character character_limiter($str, $n = 500, $end_char = '…');
Example:
<?php echo word_limiter($music[0]['title'],30); ?>
答案 1 :(得分:0)
一个简单的截断函数可以工作,如下所述:http://www.the-art-of-web.com/php/truncate/#.UW9GjLW9uSo
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
function myTruncate($string, $limit, $break=".", $pad="...")
{
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) return $string;
// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($string, $break, $limit))) {
if($breakpoint < strlen($string) - 1) {
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}
然后你的代码就是(如果你想显示最多30个字符):
<?php echo myTruncate($music[0]['title'], 30) ?>