CodeIgniter Forums
not correct work of the function character_limiter for not latin symbols - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: not correct work of the function character_limiter for not latin symbols (/thread-12127.html)

Pages: 1 2


not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]emperius[/eluser]
I've tried to use function character_limiter for cyrillic letters but it doesn't works correct, returns wrong number of symbols

with latin letter it works fine


not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]xwero[/eluser]
This is because Cyrillic characters are multibyte and php until 6 only supports single byte characters. What you can do is change the strlen function in the character_limiter with mb_strlen. The mb_* functions are not a part of the default php configuration so you have to check if they are available on your server.


not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]emperius[/eluser]
[quote author="xwero" date="1223400069"]This is because Cyrillic characters are multibyte and php until 6 only supports single byte characters. What you can do is change the strlen function in the character_limiter with mb_strlen. The mb_* functions are not a part of the default php configuration so you have to check if they are available on your server.[/quote]

it didn't help... are there any other variants?


not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]xwero[/eluser]
There is another multibyte module, iconv and it has a iconv_strlen function.


not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]emperius[/eluser]
this also didn't helped

may be I'm doing somthing wrong?

Code:
function character_limiter($str, $n = 500, $end_char = '…')
{
    if (iconv_strlen($str, 'UTF-8') < $n)
    {
        return $str;
    }
        
    $str = preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/u", " ", $str));
    //$str = preg_replace("/S+\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", ' ', $str));

    if (iconv_strlen($str) <= $n)
    {
        return $str;
    }
                                    
    $out = "";
    foreach (explode(' ', trim($str)) as $val)
    {
        $out .= $val.' ';            
        if (iconv_strlen($out) >= $n)
        {
            return trim($out).$end_char;
        }        
    }
}



not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]emperius[/eluser]
oops

I'm noticed one error


not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]xwero[/eluser]
are you sure the modules are available?


not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]emperius[/eluser]
nope

result is the same

Code:
function character_limiter($str, $n = 500, $end_char = '…')
{
    if (iconv_strlen($str) < $n)
    {
        return $str;
    }
        
    $str = preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/u", " ", $str));
    //$str = preg_replace("/S+\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", ' ', $str));

    if (iconv_strlen($str) <= $n)
    {
        return $str;
    }
                                    
    $out = "";
    foreach (explode(' ', trim($str)) as $val)
    {
        $out .= $val.' ';            
        if (iconv_strlen($out) >= $n)
        {
            return trim($out).$end_char;
        }        
    }
}



not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]emperius[/eluser]
[quote author="xwero" date="1223400941"]are you sure the modules are available?[/quote]


is there any other solution without this modules?


not correct work of the function character_limiter for not latin symbols - El Forum - 10-07-2008

[eluser]xwero[/eluser]
I don't think so