Welcome Guest, Not a member yet? Register   Sign In
working character limiter
#1

[eluser]ommy[/eluser]
Hi there,
I just noticed an error in the character_limiter (text helper).
At first i thought i made an error when calling the helper, but it seemed (and was confirmed by the forum) that the helper was faulty.
1.7 also has a problem with this helper.
Don't really know why the helper is quite massive in codeigniter, i added a much simpler version to mine temporarely.
Code:
function character_limiter($str, $n = 500, $end_char = '…')
   {
        $output = substr($str, 0, $n);
        if(strlen($str)>$n){
            $output.=$end_char;
        }
        
        return $output;
   }

If you have advice on solving this problem or information about why this helper has about 4 if functions, let me know Smile
Greets!
#2

[eluser]Derek Jones[/eluser]
http://ellislab.com/codeigniter/user-gui...elper.html

Quote:It maintains the integrity of words so the character count may be slightly more or less then what you specify

The helper's a bit smarter than a simple substr(). Perhaps if you can elaborate on what the problem is that needs to be solved - you make reference to it a few times but don't ever mention what it is?
#3

[eluser]ommy[/eluser]
That clears up a few things.
What happens is that the character limiter only limits the text if spaces occur.
So basically it behaves as a word limiter

works great with the right user input, but i heard someone say: never trust user input Smile

thanks for the lightning fast reply!
#4

[eluser]Derek Jones[/eluser]
Right, if there aren't any spaces, it will consider it as one long "word". If you're looking for a strict "has to be X characters and no more", the substr() is the way to go. This helper tries to consider the context it would be used in, cutting someone's speech to as close to X characters as it can without leaving any partial words.
#5

[eluser]ommy[/eluser]
so basically this is a little more what i would expect in a character_limiter:
Code:
function character_limiter($str, $n = 500, $end_char = '…')
    {
        $var = strstr($str, ' ');
        $bool = empty($var);
        
        
        if(!$bool){
            if (strlen($str) < $n)
            {
                return $str;
            }

            $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));

            if (strlen($str) <= $n)
            {
                return $str;
            }

            $out = "";
            foreach (explode(' ', trim($str)) as $val)
            {
                $out .= $val.' ';            
                if (strlen($out) >= $n)
                {
                    return trim($out).$end_char;
                }        
            }
        } else {
            $output = substr($str, 0, $n);
            if(strlen($str)>$n){
                $output.=$end_char;
            }

            return $output;
        }
    }
#6

[eluser]Derek Jones[/eluser]
Changes the context and behavior a bit from the way the function is described, but if those are what your needs are, that's perfectly legitimate. Were I in your shoes, I'd simply extend the Text Helper to use your customized version of the function.
#7

[eluser]ommy[/eluser]
thanks for the tips!
i'll see how that works for me
#8

[eluser]mercy loving criminal[/eluser]
The character_limiter function also seems to break encoding of the 'à' character. I've reverted to using word_limiter instead to achieve what I wanted, but would be good to see a formal fix to this - unless I've missed something.




Theme © iAndrew 2016 - Forum software by © MyBB