Welcome Guest, Not a member yet? Register   Sign In
character_limiter text helper
#1

[eluser]Coen de Jong[/eluser]
Why on earth does character limiter not actually limit on characters, but still on words? If I wanted to do that I'd have used word_limiter().
I see it could be a nice feature to not have a cut of string like this o...

But imagine this string: "I want to work in the tow-truck/crane/engineer/construction industry". Let's say I have character limit on 20 characters... CI still cuts the string after the big chunk of industries. I want to avoid this kind of words that break up the design. So I thought I'd character limit the string, but unfortunately that doesn't work as expected.

Why this decision?
#2

[eluser]markup2go[/eluser]
Because you can just use the PHP substr() function.
#3

[eluser]Coen de Jong[/eluser]
I know, but that doesn't give you nice code. substr just cuts the string regardless of length. The helper function should 1) check if the string is too long, 2) if not too long, then just return the string, 3) if the string is too long, substr it and place ... after it and return.

Now, if you'd have to do that in pure php:

Code:
if(strlen($str) > 30) echo substr($str, 0, 30) . '...'; else echo $str;

Not real nice code, when you could just do character_limiter($str, 30) in which the helper function checks on length and decides what to do with it.
#4

[eluser]sophistry[/eluser]
have a look at these two threads:
http://ellislab.com/forums/viewthread/106301/
http://ellislab.com/forums/viewthread/98155/
#5

[eluser]Coen de Jong[/eluser]
Yeah, I went ahead and just wrote these few simple lines which does just what I expect charachter_limiter() to do:

Code:
function limit($string, $max, $end_char = '…')
{

    if(strlen($string) <= $max)
        return $string;
    
    return substr($string, 0, $max) . $end_char;
}

Let's face it: a character_limiter should not cut on words. I'd be using word_limiter() instead.
#6

[eluser]n0xie[/eluser]
I don't understand exactly what the problem is.

There is nothing preventing you from writing your own implementation if the provided solution does not match your requirement. It is a framework for developers. They expect you to use the parts you find useful, and do the rest yourself.
#7

[eluser]sophistry[/eluser]
not to nitpick, but, shouldn't that be
Code:
$max-3
in the function because the ellipsis character is basically taking up 2-3 chars worth of space?
#8

[eluser]Coen de Jong[/eluser]
@n0xie and that's exactly what I've done! I wrote my own function that does that now.
I only came here with the question WHY they chose to let the function handle the input that way. You basically have two identical functions in the text helper right now, as I see it. I was only looking for an explanation.

@sophistry you're right, didn't think about that. Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB