0

I have part of code that transmit values to CSV file but in UNIX LF. But I need Windows CR LF

function insert_data($columns, $elements) {
    $elements_to_insert = count($elements);
    $count = 0;
    $message = sprintf($this->language->get('progress_export_elements_inserted'), 0, $elements_to_insert);
    $this->update_process($message);
    foreach ($elements as $element_id => $element) {
        $temp = array();
        foreach ($columns as $col_name => $col_info) {
            $custom_name = $col_info['custom_name'];
            $temp[] = array_key_exists($custom_name, $element) ? str_replace(array('\r', '\n', '/\s+/g', '/\t+/'), '', $element[$custom_name]) : '';                    
        }
        $this->writer->addRow($temp);
        $count++;
        $message = sprintf($this->language->get('progress_export_elements_inserted'), $count, $elements_to_insert);
        $this->update_process($message, true);
    }
    $this->writer->close();
}

Various attempts to transfer data to Windows CR LF are unsuccessful. There were the following attempts:

str_replace(array('\r\n', '/\s+/g', '/\t+/') 
str_replace(array('\r\n', '\n', '/\s+/g', '/\t+/') 
str_replace(array("\r\n", '/\s+/g', '/\t+/') 
str_replace(array("\r\n", "n", '/\s+/g', '/\t+/') 

How can I make it?

Alex
  • 1
  • Does this answer your question? [How to replace different newline styles in PHP the smartest way?](https://stackoverflow.com/questions/7836632/how-to-replace-different-newline-styles-in-php-the-smartest-way) – ADyson Apr 12 '21 at 08:58
  • Or this? [convert ending line of a file with a php script](https://stackoverflow.com/questions/18376167/convert-ending-line-of-a-file-with-a-php-script) – ADyson Apr 12 '21 at 09:00
  • For me, changing the code by examples is quite difficult, I will be very grateful for a specific piece of code that needs to change the existing code. – Alex Apr 12 '21 at 10:50
  • Difficult because? That's an essential skill for a programmer - taking someone else's solution and adapting it to your situation is something you normally find yourself doing almost every day. It's something you need to practice. Please be clear this site is not a free write-my-code service. If your code is answerable by pointing to an existing example then that is what will happen. There is generally little point in repeating ourselves. Please at least _try_ to solve it based on those examples. If you get stuck, post your new attempt here and explain what goes wrong. – ADyson Apr 12 '21 at 11:02

0 Answers0