I try to find a space in a string using the mb_* functions. It works for latin chars but not Chinese... I have tried to utf8_encode and iconv to utf-8 since other threads have suggested that this might be the problem.
Function that gives error is mb_strpos that returns nothing for Chinese, but a valid int for English and other latin char based texts.
I am not all that strong in encoding but assume that different encoding is causing this problem. So I am most of all asking for help on where to search since it seems that there is nothing wrong with the php-functions.
This works for both English and Chinese until mb_strpos:
// TEST THAT DID NOT WORK
//$text=iconv('ISO-8859-1','utf-8',$text);//that's NOT a solution!
//$text=utf8_encode($text);//that's NOT a solution!
// Set vars
$len = 150;
// Next to code lines are OK for both English AND Chinese
// get substring based on $len, then get length of string. Both in multibyte
$text_cropped = mb_substr($text,0,$len,'UTF-8'); // works for English AND Chinese
$string_cropped_length = mb_strlen ($text_cropped,'UTF-8');
// mb_strpos only works for English, but not for Chinese
//find last space within length in multibyte
$last_space = mb_strpos ( $text , ' ', $string_cropped_length, 'UTF-8');
// Hack to only use $last_space if mb_strpos did work
// there is an error in mb_strpos based on php version. It may return empty for chinese chars
// a work around until php upgrade is test for value of last_spave and do an if-else
if(intval($last_space) > 0) {
$text_cropped_final = mb_substr($text,0,$last_space,'UTF-8');
} else {
$text_cropped_final = $text_cropped;
}
return $text_cropped_final . '...';