1

I am trying to do a str_replace on the following line:

team 4 -1ööööö57167168

I've been able to do it in the past just by doing this:

str_replace("ööööö", " ",trim($line));

The desired outcome should be team 4 -1 57167168

However, now that I'm trying to do it in a Codeigniter application, it fails to replace it and I'm not sure what to try.

Is this an encoding config issue with Codeigniter? How can I get it to replace correctly?

Motive
  • 3,071
  • 9
  • 40
  • 63
  • does using the utf-8 escape work? ie, `str_replace("\xC3\x96", " ",trim($line));` – cegfault Aug 12 '12 at 22:35
  • 1
    Are you going [utf8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) ? Did you save your file as utf-8? Does this `echo bin2hex('ööööö');` not give you `c3b6c3b6c3b6c3b6c3b6`? – Esailija Aug 12 '12 at 22:40
  • @cegfault nope, didn't seem to work :( – Motive Aug 12 '12 at 22:40
  • @Esailija I think so, the file is generated by the game itself and I have no control over it's encoding. – Motive Aug 12 '12 at 22:42
  • @MotiveKyle please post the `bin2hex` of both strings. – Esailija Aug 12 '12 at 22:44
  • Verify that the file is utf8 (e.g. in mac osx terminal run `file -I yourinputfile`). – mAu Aug 12 '12 at 22:44
  • @Esailija the bin2hex for ö is returning `c3b6` – Motive Aug 12 '12 at 22:48
  • @MotiveKyle ok cool, then your file is saved as utf8. The string you are getting is not, where are you getting it from? From a database? What was the actual outcome, btw. – Esailija Aug 12 '12 at 22:49
  • @Esailija I'm just uploading the file using Codeigniter's file uploading class and just using fopen to read it from the folder. The file is a log of player actions in a game (for watching replays). I'm not pulling anything from a database yet. Sorry, what do you mean by the actual outcome? – Motive Aug 12 '12 at 22:56
  • I mean what you see as a result? You could also post the `bin2hex` of the file. if you see it givin c3b6 for `ö` then your issue isn't with encoding at all. – Esailija Aug 12 '12 at 22:58

2 Answers2

1

Checkout the multi byte string functions here http://php.net/manual/en/ref.mbstring.php and specifically the mb_str_replace.

Note, ö can be represented in multiple character sets so make sure you are matching the correct character set!

williamvicary
  • 805
  • 5
  • 20
-3

Use utf8_encode() and utf8_decode(), PHP isn't a UTF8 native language !

  • This is pretty harmful advice because there is no need for these conversions (You can just work with utf8) and you will lose out on 99.99% of the unicode characters because ISO-8859-1 cannot encode them. For example, [`utf8_decode("€")` gives `?`](http://codepad.viper-7.com/CisDx3) and there is no way to know from that question mark (`?`) that the original character was `€` – Esailija Aug 12 '12 at 22:59