Permalink
Browse files

Just normalize all line endings, rather than detecting.

  • Loading branch information...
1 parent 943fc4f commit c7d857b6fa9fcdddd78265b232572d1a02efa0f4 @benbalter benbalter committed Jan 5, 2013
Showing with 13 additions and 17 deletions.
  1. +13 −17 class.csv-to-api.php
View
@@ -152,29 +152,25 @@ function xml_entities( $string ) {
}
-
+ /**
+ * Normalize all line endings to Unix line endings
+ * @param string the mixed-ending string to normalized
+ * @return string the normalized string
+ */
+ function normalize_line_endings( $string ) {
+ $string = str_replace( "\r\n", "\n", $string );
+ $string = str_replace( "\r", "\n", $string );
+ return $string;
+ }
+
/**
* Turn CSV into a PHP array.
*/
function parse_csv( $csv ) {
- /*
- * Determine which character to use to break up lines based on which one is the
- * most common. If they're both just as common, then they're Windows newlines.
- */
- $newlines_unix = substr_count($csv, "\n" );
- $newlines_mac = substr_count($csv, "\r" );
- if ( $newlines_unix > $newlines_mac ) {
- $newline = "\n";
- }
- elseif ($newlines_unix == $newlines_mac) {
- $newline = "\r\n";
- }
- else {
- $newline = "\r";
- }
+ $csv = $this->normalize_line_endings( $csv );
- $lines = explode( $newline, $csv );
+ $lines = explode( "\n", $csv );
$lines = $this->parse_lines( $lines );
$headers = array_shift( $lines );

1 comment on commit c7d857b

@waldoj
Contributor
waldoj commented on c7d857b Jan 6, 2013

...

Oh. Right.

Please sign in to comment.