Answer the question
In order to leave comments, you need to log in
The substr and strlen function in php does not work correctly with Russian characters. How to solve the problem?
The substr and strlen function in php does not work correctly with Russian characters (utf8 encoding). Tried mb_substr also - didn't help.
Who will help solve the problem?
Answer the question
In order to leave comments, you need to log in
Here is an example function:
/**
* 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;
}
If $fultext is in English letters, everything is ok. Reduces. If Russian - empty string
1. Use mb_substr, mb_strlen with $encoding='utf-8'
2. Don't use $char=$fulltext{0}, replace with mb_substr
iconv_substr($s, $from, $len, 'UTF-8')
iconv_strlen($s, 'UTF-8')
$s{$i} ==> iconv_substr($s, $i, 1, 'UTF- eight')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question