donglu2008 2016-01-13 16:14
浏览 61
已采纳

如何在codeigniter视图中截断字符串

here is my controller which will get the url information from some model function and passed it to my view.

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);

    }
}

and my view is

    <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>

but it shows full url. can I use character_limiter in view? or I have to truncate it in my controller and passed it to view?

  • 写回答

1条回答 默认 最新

  • dsebywql016137 2016-01-13 16:24
    关注

    In this case you had to use ellipsize() function from text helpers

    This function will strip tags from a string, split it at a defined maximum length, and insert an ellipsis.

    The first parameter is the string to ellipsize, the second is the number of characters in the final string. The third parameter is where in the string the ellipsis should appear from 0 - 1, left to right. For example. a value of 1 will place the ellipsis at the right of the string, .5 in the middle, and 0 at the left.

    An optional forth parameter is the kind of ellipsis. By default, … will be inserted.

    $str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';
    
    echo ellipsize($str, 32, .5);
    

    Produces:

    this_string_is_e…ak_my_design.jpg
    

    ellislab.com official documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 halcon根据玻璃面板纹路取区域
  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥50 webrtc-streamer TCP rtsp
  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题