The substr function and strlen in php is not correct work with Russian characters. How to solve the problem?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
17 views
The substr function and strlen in php is not correct work with Russian characters (utf8 encoding). I tried mb_substr also — did not help.


Who will help to solve the problem?
by | 17 views

5 Answers

0 like 0 dislike
and what is the impropriety then, you can learn ???
by
0 like 0 dislike
Here for example function:
\r
/**
* shortenText
* @param string
* @param int
* @param bool, default true
* @since 1.0.9
*/
function shortenText( $text, $count, $addbb=true )
{
if( $count > 0 )
{
$totalchars = strlen( $text );
$charcount = 0;
$fulltext = $text;
$buffer = ";
if( $totalchars >= $count )
{
$bbcode = false;
while( strlen( $fulltext ) > 0 )
{
$char = $fulltext{0};
if( $char == '[' ) $bbcode = true;
if( ( $bbcode && $addbb ) || $charcount < $count ) $buffer .= $char;
$fulltext = substr( $fulltext, 1 );
if( !$bbcode ) $charcount++;
if( $char == ']' ) $bbcode = false;
}
$text = $buffer.( $charcount >= $count? '...': ");
}
}
return $text;
}
\r
If $fultext — English letters. Reduces. If the Russian is an empty string
by
0 like 0 dislike
1. Use mb_substr, mb_strlen with parameter $encoding='utf-8'
2. Do not use $char=$fulltext{0}, replace mb_substr
by
0 like 0 dislike
iconv_substr($s, $from, $len, 'UTF-8')
iconv_strlen($s, 'UTF-8')
$s{$i} ==> iconv_substr($s, $i, 1, 'UTF-8')
by
0 like 0 dislike
You have $bbcode always $false, is this normal? Code is certainly fun...
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Aug 2, 2019 by Malkolm163
110,608 questions
257,187 answers
0 comments
40,330 users