Line break problem and subster in php

Asked

Viewed 330 times

2

I made a rectangle that contains the title of the article, this title is limited using the replace php, but when a word is large it breaks the line by continuing the text below, but the number of characters remains the same, only the text ends up getting larger than rectangle because of the line break. Someone could help me solve, I don’t want you to enlarge the rectangle.

2 answers

1

See if this function would help in something:

 function limit_chars($string,$caracteres = 100)
{
     $string = strip_tags($string);
    if (strlen($string) > $caracteres) {
        while (substr($string,$caracteres,1) <> ' ' && ($caracteres < strlen($string))){
            $caracteres++;
        };
    };
    if (strlen(substr($string,0,$caracteres)) < $caracteres){
        return substr($string,0,$caracteres);
    }else{
        return substr($string,0,$caracteres)."...";
    }

}

So with this function you will be able to limit the number of characters and still have "..." at the end. I hope I helped :D

0

I believe that what you need is solved easily with

<!DOCTYPE html>
<html>
<head>
<style> 
div.test {
    white-space:nowrap; 
    width:12em; 
    overflow:hidden; 
    border:1px solid #000000;
}
</style>
</head>
<body>
    <div class="test" style="text-overflow:ellipsis;">Este é apenas um texto longo para exemplificar o funcionamento</div>    
</body>
</html>

with that the result will be Este é apenas um texto lon...

You can see the example working here and see all usage settings of text-overflow here.

  • It works, but I need something that works in ie7.

  • 1

    For this I recommend a plug [tag:jquery] http://dotdotdotdot.frebsite.nl/

Browser other questions tagged

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