Problem with line break and substr in php

2

I made a rectangle containing the article title, that title is limited using php substr , but when a word is large it breaks the line by continuing the text below, however the number of characters remains the same, except that the text ends up being larger than the rectangle because of the line break. Could someone help me as a solution, I do not want it to raise the rectangle.

    
asked by anonymous 11.03.2014 / 21:24

2 answers

1

See if this function would help with 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 it will be possible to limit the amount of characters and still have "..." at the end. I hope I have helped: D

    
12.03.2014 / 13:54
0

I think what you need is easily solved 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 this the result will be Este é apenas um texto lon...

You can see the example working here and see all usage settings for text-overflow < a href="https://developer.mozilla.org/en/docs/CSS/text-overflow"> here .

    
11.03.2014 / 21:41