The original code for which everything works as expected. There is a certain line $finded , I make the first letter big and add to the database.

 //делаем первую буква большой $char = mb_strtoupper(substr($finded, 0, 2), "utf-8"); // это первый символ $finded[0] = $char[0]; $finded[1] = $char[1]; //второй //далее вставляем в базу $this->db->insert($sql, $params); 

Then he added the cleaning of multiple spaces in $finded and the PDO reports an error, the encoding incompatibility is apparently

 array(3) { [0]=> string(5) "HY000" [1]=> int(1366) [2]=> string(82) "Incorrect string value: '\x9E\xD1\x82\xD0\xBF\xD1...' for column 'status' at row 1" } 

This is done before the code above, the modifier u in the regulars does not help

 $finded = preg_replace('/\s\s+/isu', ' ', $finded); 

What is wrong here?

  • Judging by this, '\x9E\xD1\x82\xD0\xBF\xD you have lost the first byte \xD0 . - Visman

1 answer 1

 <?php mb_internal_encoding("UTF-8"); $finded = 'шел по лесу ежик'; $finded = mb_strtoupper(mb_substr($finded, 0, 1)) . mb_substr($finded, 1); $finded = preg_replace('/\s+/u', ' ', $finded); var_dump($finded); 

Will return

 string 'Шел по лесу ежик' (length=29) 
  • I misunderstand the number of characters in the functions str and mb_ ? - Jean-Claude
  • @ Jean-Claude, are you sure that the first character in your line is always double-byte? Why use the function for single-byte characters, if there is a special function for multi-byte characters? - Visman
  • Originally it was not mine) - Jean-Claude
  • senks. I'll deal with multibyte - Jean-Claude