2016-01-13 78 views
0

這裏是我的控制器,它將從某個模型函數獲取URL信息並將其傳遞給我的視圖。如何截斷codeigniter視圖中的字符串

class MyController extends CI_Controller{ 

    public function __construct() 
    { 
     parent::__construct(); 

     //load the settings model 
     //some model 

     //load the text helper 
     $this->load->helper('text'); 
    } 

    public function index() 
    { 

     //call the model function to get the Url data 
     $data['urllist'] = //call the model function and get the array and store it to urllist; 

     //load the view 
     $this->load->view("myview",$data); 

    } 
} 

和我的看法是

<body> 
     <?php 

      /* 
       //$longurl is an array element and its value is 
       some thing like 
       http://example.com/sdsds/sdsdsd/sdsdsdsd/sdsdsd/sdsdsd 

       i want to truncate it about 20 characters 
      */ 
      $lurl=character_limiter($longurl, 20); 
      echo $lurl; 

     ?> 
    </body> 

,但它顯示完整的URL。我可以在視圖中使用character_limiter嗎?或者我必須截斷它在我的控制器,並通過它來查看?

+0

不要在一個控制器的所有邏輯。 –

+0

什麼意思關於truncate? –

回答

0

在此情況下就必須用ellipsize()函數從文本助手

該功能將從字符串中去除標記,在限定的最大長度拆分它,並插入省略號。

第一個參數是字符串到ellipsize,第二個是 最後一個字符串中的字符數。第三個參數是 中的字符串省略號應該從0到1出現,從左到右。例如, 。值爲1會將省略號放在字符串 的右側,中間爲.5,左側爲0。

可選的第四個參數是省略號的種類。默認情況下, &hellip;將被插入。

$str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg'; 

echo ellipsize($str, 32, .5); 

產地:

this_string_is_e…ak_my_design.jpg 

ellislab.com official documentation

+0

謝謝工作。 –