-3

I have a very annoying problem with the euro symbol in my PHP-array. I would like to split strings of different lengths by every character into arrays.
E.g.: "Hi guys" ==> array("H", "i", " ", "g", "u", "y", "s")

This works great except with the € symbol. As soon as a "€" is in the string it is output as "�". I have already tried a lot, but unfortunately I can't get any further.

UTF-8 is set as default-charset in php.ini.

Do you have a tip for me?

This is a simple example:

<?php 
  $euro = "€";
  $split = str_split($euro);
  echo $split[0]; // Ouput in browser: �
?>

Thanks a lot!

I have already researched for hours on the Internet and tested the various functions...

  • 2
    Try https://www.php.net/manual/en/function.mb-str-split.php instead – Gordon Aug 10 '23 at 09:54
  • 2
    Are you ouputting this to a web page? Is the web page enabled for UTF-8 (e.g. using ``? And/or you can set `header('Content-Type: text/html; charset=utf-8');` as a http response header in PHP, to indicate this to the browser. Reference: https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – ADyson Aug 10 '23 at 09:56
  • 1
    @Gordon awesome, this realy fixed my problem. Thank you very much!! Unbelievable. I did not get this simple solution myself. Have a nice time! – Dennis Möbus Aug 10 '23 at 10:34

1 Answers1

3

The symbol takes 3 bytes when encoded in UTF-8 (E2 + 82 + AC). PHP string functions typically operate on byte level unless otherwise noted. You need a multi-byte aware alternative. str_split() manual page literally warns about this:

Note:

str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string. Use mb_str_split() to split the string into code points.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360