Welcome Guest, Not a member yet? Register   Sign In
not correct work of the function character_limiter for not latin symbols
#1

[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
#2

[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.
#3

[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?
#4

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

[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;
        }        
    }
}
#6

[eluser]emperius[/eluser]
oops

I'm noticed one error
#7

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

[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;
        }        
    }
}
#9

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


is there any other solution without this modules?
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB