0

In my database I have a serialized string containing this data: Boy’s, the character ’ is a 3-byte character, causing the serialized string to look like: s:7:"Boy’s", however, when I get it in PHP it says Boy?s, which is only 5 characters long - making the serialized string wrong.

Is there a way I, on the output, can convert the ? to a ’ in the string to resolve the unserialize problem?

The dataformat in the Database is utf8mb4_unicode_ci and cannot be changed.

I have tried, and figure the problem lies somewhere around a, utf8_decode or utf8_encode, but I cannot seem to make it work

I hope some of you can answer my problem

  • Is this a [UTF-8 All the way through issue](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – RiggsFolly Jun 14 '17 at 11:45
  • When using multibyte characters, there's a couple of things to go over to make sure you actually set the proper charset. I have previously written [**an answer**](http://stackoverflow.com/questions/31897407/mysql-and-php-utf-8-with-cyrillic-characters/31899827#31899827) that contains a little checklist, that will cover *most* of the charset issues in a PHP/MySQL application. There's also a more in-depth topic, [UTF-8 All the Way Through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through). Most likely, you'll find a solution in either one or both of these topics. – Qirel Jun 14 '17 at 11:45
  • @RiggsFolly I am quite certain its proper UTF-8 all the way, as it is displayed properly in the database and other places on the website; Qirel will do – Troels M. B. Jensen Jun 14 '17 at 11:49
  • If its stored properly in the database, it could possibly be HTML/PHP header, connection object (not very likely if its stored correctly) or the file-encoding. It's worth checking every possibility - and set everything to UTF8 even if it appears to be correct. Then you're sure you're not mixing any charset anywhere. – Qirel Jun 14 '17 at 11:51
  • Is your script file encoded utf-8? – RiggsFolly Jun 14 '17 at 11:52
  • @Qirel I have gone through your checklist, and included what I missed, but it still does not seem to work properly, though set_charset doesn't seem to work: $this->connection = new \PDO(xx) - $this->connection->set_charset("utf8"); – Troels M. B. Jensen Jun 14 '17 at 11:53
  • `set_charset()` is a `mysqli_*` method. You need to add `;charset=utf8` in your DSN. Look at the bottom of the checklist. – Qirel Jun 14 '17 at 11:54
  • @Qirel I use PDO and I get this error PHP Fatal error: Uncaught Error: Call to undefined method PDO::set_charset() – Troels M. B. Jensen Jun 14 '17 at 11:55
  • @Qirel I googled the proper function name for PDO (Which I didnt do before cuz I am a stupid being), and used that instead which made it work - thank you for the help though! – Troels M. B. Jensen Jun 14 '17 at 11:56
  • And also thanks for the help @RiggsFolly! – Troels M. B. Jensen Jun 14 '17 at 11:57
  • @TroelsM.B.Jensen Like the footnote in my checklist (and my edited comment above) says, you need `;charset=utf8` in the DSN string. `set_charset()` is a MySQLi method and won't work with PDO. Glad you've solved your issue though! – Qirel Jun 14 '17 at 11:58
  • @Qirel I didnt see that you edited your comment before I googled it - they both solve the dilemma though, which is nice – Troels M. B. Jensen Jun 14 '17 at 11:59

0 Answers0