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 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决