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/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: not correct work of the function character_limiter for not latin symbols (/showthread.php?tid=12127)

Pages: 1 2


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

[eluser]emperius[/eluser]
I've checked mb-string is set up on the hosting and is working for usual stings but for the stings taken from database the number of symbols is returned wrong

here is the code of the character_limiter function
Code:
function character_limiter($str, $n = 500, $end_char = '…')
{
    if (mb_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 (mb_strlen($str) <= $n)
    {
        return $str;
    }
                                    
    $out = "";
    foreach (explode(' ', trim($str)) as $val)
    {
        $out .= $val.' ';            
        if (mb_strlen($out) >= $n)
        {
            return trim($out).$end_char;
        }        
    }
}


and here is the example of my code

Code:
$query = mysql_query("SELECT Name FROM Table");
if(mysql_num_rows($query)>0)
{
   $arr = mysql_fetch_array($query);
   echo character_limiter($arr['Name'], 10);
}