如何在代码点火器视图中截断字符串


How to truncate a string in codeigniter view

这是我的控制器,它将从一些模型函数中获取url信息并将其传递给我的视图。

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-1从左到右出现。例如值为1时,省略号将位于字符串,中间为0.5,左边为0。

可选的第四个参数是省略号的类型。默认情况下,…将插入。

$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官方文档