当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to concatenate text with dynamic length in CODEIGNITER?

问题描述:

How do I concatenate the expected text with varying length?

I have already placed $this->load->helper('text'); in my controller. The closest example shown in ellislab page is:

$str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';

echo ellipsize($str, 32, .5);

According to the explanation, this will produce: this_string_is_eak_my_design.jpg .

However, in my case, I do not have a fixed string to be concatenated. What I need to concatenate is a description element which length can vary depending on user input.

<td class="first" width="200px"><a href="<?= site_url() ?>/provider/view_job/<?= $row['job_id'] ?>"><?= $row['description'] ; ?></a></td>

How should I adjust line to have its result concatenated?

Thank you for any possible help here.

网友答案:

Then you can use a PHP function strlen: http://us2.php.net/strlen

strlen Get string length

so for your code / example you can do this:

ellipsize($str, strlen($str)/2, .5);

or in your case:

<?= ellipsize($row['description'], strlen($row['description'])/2, .5); ?>

Also if you don't want to use CodeIgniter's function, you can use PHP's sub_str: http://us3.php.net/substr

substr("abcdef", 0, -1);  // returns "abcde"

So for your code it would be something like:

 <?= substr($row['description'], 0, strlen($row['description'])/2) . '...'; ?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: