2016-01-13 81 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

谢谢工作。 –