限制从数组编码器返回的文本的大小


Limit the size of text being echod from an array Codeigniter?

在我的CI应用程序中,我从数组显示文本:

<?php echo $music[0]['title']?>

限制文本大小的最简单方法是什么?我想限制文本的字符或像素大小。我尝试使用CSS,但它只适用于实际文本。

    //IN CONTROLLER
    $this->load->helper('text');
    //In view file
    //For Word Limiter use word_limiter($str, $limit = 100, $end_char = '&#8230;');
    //FOr Character character_limiter($str, $n = 500, $end_char = '&#8230;');
    Example:
    <?php echo word_limiter($music[0]['title'],30); ?>

一个简单的截断函数就可以了,如下所述: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) ?>