Character limit

Asked

Viewed 889 times

2

I need to limit characters in a text, and should keep (...). The title is sticking out of the layout.

if($related){
    echo '
    <div class="row">
        <div class="medium-12 columns">
            <div class="ap-ads-related">
                <h2>Anúncios Relacionados</h2>
                <div class="row">
    ';

    foreach ($related as $key => $rel) {
        $related_images = $this->ads_model->images($rel->ad_id);
        $related_image = thumbnail(@$related_images[0]->ads_img_file, "ads", 260, 180, 2);

        echo '
            <div class="small-6 medium-3 end columns">
                <a href="'.base_url('anuncio/'.$rel->ad_slug).'" title="'.$rel->ad_name.'" class="item">
                    <div class="image"><img alt="'.$rel->ad_name.'" src="'.$related_image.'"></div>

                    <h4>'.$rel->ad_name.'</h4>

                    <div class="price">'.(($rel->ad_service) ? 'Serviço' : string_money($rel->ad_price)).'</div>
                </a>
            </div>
        ';
    }
    echo '
                </div>
            </div>  
        </div>
    </div>
    ';
}

?>

4 answers

6

Codeigniter itself has a function for this, I believe it should be using because it tagged the same in the question. You can limit by characters or by words, follow code:

<?php print character_limiter($rel->ad_name, 25); ?>

Will limit by 25 characters and after this will insert ...

<?php print word_limiter($rel->ad_name, 25); ?>

Will limit by 25 words and after will advise ...

If you have any questions, please follow the Codeigniter link with some information: https://codeigniter.com/userguide3/helpers/text_helper.html

  • @Willian I did not research a solution with Codeigniter, my fault, I made a manual solution using a helper, but the solution here of the partner does not reinvent the wheel. (+1) Which doesn’t mean mine isn’t useful, but this one was actually better

  • I’m kind of a layman in codeigneiter, could you pass the complete code correctly? I wanted to see if it works

  • Just insert in place of $rel->ad_name this: print character_limiter($rel->ad_name, 25), do the test.

1

I created a helper called my_strings.php in the briefcase Application/Helpers

<?php

  function cortaTexto($string, $maxLength){
     if(strlen($string) <= $maxLength)
        return $string;

     return substr($string, 0, $maxLength) . '...';
  }

?>

Add this helper to autoload.php,

$autoload['helper'] = array('html', 'url', 'my_strings');

So in View:

cortaTexto($rel->ad_name, 30)

1

Dude, you can also solve this with css.

Here are the links that helped me when I needed,

http://www.w3schools.com/cssreF/css3_pr_text-overflow.asp

https://css-tricks.com/snippets/css/truncate-string-with-ellipsis/

https://stackoverflow.com/questions/26973570/setting-a-max-character-length-in-css

UPDATE

Set the container width and place text-overflow: ellipsis and when you reach the limit of the container the text will be automatically replaced by ...

See example taken from css-Tricks

.truncate {
  width: 250px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

0

Very simple example:

$string = 'String para ser reduzida'
$reduzido = substr_replace($string, (strlen($string) > 42 ? '...' : ''),42);

I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.