如何在codeigniter视图中截断字符串(How to truncate a string in codeigniter view)

编程入门 行业动态 更新时间:2024-10-10 21:26:30
如何在codeigniter视图中截断字符串(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>

但它显示完整的网址。 我可以在视图中使用character_limiter吗? 或者我必须在我的控制器中截断它并传递给它查看?

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?

最满意答案

在这种情况下,您必须使用文本助手中的ellipsize()函数

此函数将从字符串中剥离标记,将其拆分为定义的最大长度,并插入省略号。

第一个参数是ellipsize的字符串,第二个参数是最终字符串中的字符数。 第三个参数是字符串中的省略号从0到1,从左到右出现的位置。 例如。 值为1会将省略号放在字符串的右边,.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官方文档

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

更多推荐

本文发布于:2023-08-04 01:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   字符串   如何在   codeigniter   view

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!