views:

287

answers:

7

Hi there,

how do I fit long text into fixed with column where I have place for only line of text? I would need to shorten the text to fixed width (lets say to 100px) and I would like to add dots "..." at where string gets cut.

Something like this for example: My string is "Some really long string that I need to fit in there" and output in a fixed width column would look like this

|Some really long string...|

Thanks, Jume

+1  A: 
adam
This doesn't work right, since the string can have more or less CAPS or wider character... so if you take substring of fixed character (100 as you suggest) can output of different width when page renders. Try strings like:MMMMMMMMMMMMMMMMMMMM oriiiiiiiiiiiiiiiiiiiand you see you already have a difference.
Primoz Rome
Hence what I said about outputting text in a fixed-width font.
adam
A: 

I had a similar problem, the way I solved it was to strip the string down to 60 characters and append an '...' to the end of it.

An ugly solution? Yes, but unless there is a jQuery solution it's probably your best bet.

If you're using Smarty, this is how I solved the problem:

{$my_string|truncate:60}
ILMV
A: 

You can use PHP's wordwrap function for that :)

Example:

$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");

echo $newtext;
Sarfraz
The OP wants to truncate the text rather than wrap it (although I agree wrapping might make more sense in some cases)
adam
Tried this but it doens't give me the result I want! I want to fit the text into width of 100 pixels and not fixed number of characters...
Primoz Rome
A: 

Use substr and find out how many characters fit into the column. E.g.

$string = substr($string, 0, 10) . '...'

if 13 characters fit into the column

Felix Kling
You cannot find out how many characters fit into a column. It depends on the font used in the browser and the zoom level of the page.
Gordon
@Gordon you're assuming the output is html. What if this is a command line script?
adam
@adam Why would the question be tagged CSS then?
Gordon
A: 

Here's a function I use to truncate strings. Like most of the suggestions here, it uses substr to truncate the string, but it will avoid splitting the string mid-word:

function truncate_text($string, $min_chars, $append = ' &hellip;') {
    $chars = strpos($string, " ", $min_chars);
    $truncated_string = substr($string, 0, $chars) . $append;
    return $truncated_rstring;
}
David Heggie
A: 

I think simple cutting text after N characters isn't what you're looking for. It's not a solution because the following text have both 15 character length: iiiiiiiiiiiiiii, mmmmmmmmmmmmmmm - notice that the second "word" is about three times longer than the first one.

JavaScript might be a solution:

  1. First prepare your mark-up:

    <p id="abc">{TEXT}</p>
    

    Where {TEXT} is your text truncated to 150 characters + ...

  2. Now when we've got a good base for JavaScript we can try to make what you're looking for:

    <html>
    <head> 
        <style type="text/css">
            #abc {
                width: 100px;
            }
        </style>
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded", function() {
                var ref  = document.getElementById("abc");
                var text = ref.text;
    
    
    
            ref.removeChild(ref.firstChild);
            ref.appendChild(document.createTextNode("..."));
    
    
            var maxHeight = ref.offsetHeight;
    
    
            var pos = 0;
            while (ref.offsetHeight &lt;= maxHeight) {
                var insert = text.substring(0, ++pos) + "...";
    
    
                var finalReplace = ref.replaceChild(document.createTextNode(insert), ref.firstChild);
            }
    
    
            ref.replaceChild(finalReplace, ref.firstChild);
        }, false);
    &lt;/script&gt;
    
    </head> <body> <p id="abc">My very, very, very, very, very, very, very long text...</p> </body> </html>
Crozin
+3  A: 

You can do this with CSS alone:

.box {
    -o-text-overflow: ellipsis;   /* Opera */
    text-overflow:    ellipsis;   /* IE, Safari (WebKit) */
    overflow:hidden;              /* don't show excess chars */
    white-space:nowrap;           /* force single line */
    width: 300px;                 /* fixed width */
}

Check out this article about the CSS3 property text-overflow. Firefox currently (3.5.7) does not support this yet, but there is some workarounds.

Gordon
But this wont add me "..." at the end of the string...
Primoz Rome
Yes, it will. Read through the pages, I've linked for compatibility across browsers and workarounds.
Gordon
Ahh sorry. Thanks man, looks promising.
Primoz Rome