21

I'm working with a CSV file which is exported from Excel.

I have a column that contains a value of 1 234,00. I need to get all whitespaces away from these kinds of columns with PHP and I've tried to do it with preg_replace("/\s*/","",$column) as well as with str_replace(" ","",$column). I was almost ready to lose it so I took a glance into the csv-file with a HEX-editor and noticed, that this space consist of two hex values, C2 and A0 which seems to be UTF-8 non-breaking space.

But I suck with encoding stuff and I'm still confused in finding a way to remove them. Any ideas?

BudwiseЯ
  • 1,846
  • 2
  • 16
  • 28

2 Answers2

48
$column = str_replace("\xc2\xa0", '', $column);
phihag
  • 278,196
  • 72
  • 453
  • 469
  • To whom it may be interested this works because the input is UTF-8, if the input is ISO-8895-1 or CP1252 you must use `str_replace("\xA0", '', $column);` Thanks to https://stackoverflow.com/a/33019796/260080 – Marco Demaio Feb 10 '19 at 21:19
  • @MarcoDemaio Or better first, [convert to UTF-8 first](http://php.net/manual/en/function.utf8-encode.php). Any application that works with ISO-8859-1 internally is buggy – for instance, it will not correctly handle emojis. – phihag Feb 11 '19 at 07:45
  • Beware that if `$column` was `null`, it will become an empty string. – at54321 Aug 02 '21 at 06:52
16

You may use trim

trim($data['value'], " \t\n\r\0\x0B\xc2\xa0")

Where \t\n\r\0\x0B is defualt mask, \xc2\xa0 need add

Andrey Vorobyev
  • 896
  • 1
  • 10
  • 37
  • 2
    [trim](http://php.net/manual/en/function.trim.php) also includes space in the default mask — `" \t\n\r\0\x0B\xc2\xa0"`. – galva Mar 05 '16 at 01:52
  • 1
    This might remove parts of an otherwise correct multibyte character. `var_dump("\xc2\xb4AAAA"); var_dump(trim("\xc2\xb4AAAA", " \t\n\r\0\x0B\xc2\xa0"));` – 0x6368 Nov 07 '19 at 17:26