6

I have a CSV I am downloading from a source I'm not in control of and the end of each line is a

^M 

character when printed to a bash terminal. How can I sanitize this input programmatically in PHP?

nsfyn55
  • 14,875
  • 8
  • 50
  • 77

3 Answers3

20

What you're seeing is a Windows control character. To get rid of this in PHP, what you need to do is $file = str_ireplace("\x0D", "", $file) this will work whether hexadecimal is lowercase or uppercase.

jarriett
  • 1,219
  • 12
  • 7
5

You can also ask PHP to auto detect any weird line endings by just adding in this line before reading the CSV file and you won't be required to do anything else.

ini_set('auto_detect_line_endings', true);
Kamal Soni
  • 1,522
  • 13
  • 15
  • Valid approach, but did not work for me, which is a bit weird. I ended up replacing win with linux chars: preg_replace('~\r\n?~', "\n", $string); – Ivan Jovović Feb 02 '18 at 12:50
  • Ok, I think it might be possible you have a different OS to mine. I am using Mac, so it might not be working on all OS. It is weird as you said. – Kamal Soni Feb 03 '18 at 09:40
4

^M is a carriage return, you should be able to remove it with:

$string = str_replace( "\r", "", $string);
nickb
  • 59,313
  • 13
  • 108
  • 143