3

Hello !

I am getting a String from a .xlsx file ->"N°X"

I would like to replace "N°" by Num_X

I am trying to do this with

$var = str_replace("N°","Num_",$var);

But nothing is replaced (according to echo $var) problem come from ° because when i try to replace some String by other (without °) str_replace works

Any suggestions ?

Barmar
  • 741,623
  • 53
  • 500
  • 612
John Snow
  • 77
  • 2
  • 11

1 Answers1

1

Ensure that input string is UTF8.

$var = "N°X";
print mb_detect_encoding($var);

If you don't get UTF-8 out of this, convert it:

$var = mb_convert_encoding($var, 'UTF-8');

And then your str_replace will work as intended.

Another tool that might help you with encoding issues is xxd.

php -r '$var = "N°X"; echo $var;' | xxd

should return

00000000: 4ec2 b058                                N..X

which reveals the middle character is encoded as C2B0 hex, which is Unicode Character 'DEGREE SIGN' (U+00B0). fileformat.info comes handy now.

  • Also beware of unicode [NUMERO SIGN](http://www.fileformat.info/info/unicode/char/2116/index.htm) character (№). – Vojtěch Pithart Jun 21 '17 at 17:47
  • Oops thought i would get some notifications if i get some answers Yes that's what i did $var = mb_convert_encoding ($var,"Windows-1252", "UTF-8"); and it works :) Thanks – John Snow Jun 22 '17 at 17:56