CSV new Line Character

12,381

PHPs file functions do not reliably recognize line endings from Macs (if they use \r character, as Mac Excel does, if I recall correctly.)

You need to set auto_detect_line_endings in php.ini to true, then it will properly recognize line endings, whether they are \n, \r, or \r\n.

This setting is PHP_INI_ALL, which means that it can be set anywhere, i.e., in php.ini, in .htaccess for a particular directory, or even with ini_set, e.g., ini_set('auto_detect_line_endings', true)

Share:
12,381
Harsha M V
Author by

Harsha M V

I turn ideas into companies. Specifically, I like to solve big problems that can positively impact millions of people through software. I am currently focusing all of my time on my company, Skreem, where we are disrupting the ways marketers can leverage micro-influencers to tell the Brand’s stories to their audience. People do not buy goods and services. They buy relations, stories, and magic. Introducing technology with the power of human voice to maximize your brand communication. Follow me on Twitter: @harshamv You can contact me at -- harsha [at] skreem [dot] io

Updated on August 28, 2022

Comments

  • Harsha M V
    Harsha M V over 1 year

    I have an Excel Template that i use to fill in data and export that to CSV to populate the following page on my website.

    http://play.mink7.com/ifocus_v4/careers.php

    When i export the file in Windows i get the formatting for the new line charcter right. But when i export the same excel file from MAC i dont get the new line character.

    Windows File
    play.mink7.com/ifocus/win.csv
    

    and

    MAc File
    play.mink7.com/ifocus/mac.csv
    

    .

    My Code

    <?php 
    
    include_once("libs/csv2json/CSVParser.php");
    
    $jsonParser = CSVParserFactory::Create("json");
    $path = "uploads/jobs.csv";
    
    //This property will use the first row to convert the results into a
    //json object array
    $jsonParser->IsFirstRowHeader = true;
    $jobs = $jsonParser->Parse($path);
    
    $jobs = json_decode($jobs, true);
    
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>iFocus - Careers</title>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,400,700,300' rel='stylesheet' type='text/css'>
    <script src="js/modernizr.custom.34639.js"></script>
    <script src="js/jquery-1.9.1.min.js"></script>
    <script src="js/jquery.accordion.min.js"></script>
    <link rel="stylesheet" href="css/normalize.min.css">
    <link rel="stylesheet" href="css/styles.css">
    </head>
    
    <body>
    <div id="header" class="wrapper">
      <header class="container">
        <div id="logo"><a href="index.php"><img src="img/logo.png" width="185" height="69" alt="ifocus logo"></a></div>
        <nav id="primaryNav">
          <ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="about_us.php">About Us</a></li>
            <li><a href="case_studies.php">Case Studies</a></li>
            <li><a href="services.php">Services</a></li>
            <li><a href="testimonials.php">Testimonials</a></li>
            <li><a class="active" href="careers.php">Careers</a></li>
            <li><a href="contact_us.php">Contact Us</a></li>
          </ul>
        </nav>
        <div class="clearfix"></div>
      </header>
    </div>
    <div id="mainBody">
      <div class="container">
        <h3>Careers</h3>
        <h4>iFocus is looking for experienced professionals in the following areas: </h4>
        <p>Email your resume to [email protected]</p>
        <div class="active-jobs">
    
        <?php $i = 1; ?>
        <?php foreach($jobs as $j): ?>
          <h3 class="accordion" id="nav-section<?php echo $i++; ?>"><?php echo $j['job_code']." - ".$j['title']; ?><span></span> </h3>
          <div class="accordion-content">
            <p><strong>Relevant Experience:</strong> <?php echo $j['experience']; ?></p>
    
            <p><strong>Job Description::</strong></p>
            <?php $job_desc = explode("\n", $j['job_desc']);?>
            <ul>
              <?php foreach($job_desc as $jd):?>
              <li><?php echo $jd; ?></li>
              <?php endforeach; ?>
            </ul>
    
            <p><strong>Skill Set:</strong></p>
            <?php $skillsets = explode("\n", $j['skillsets']);?>
            <ul>
              <?php foreach($skillsets as $s):?>
              <li><?php echo $s; ?></li>
              <?php endforeach; ?>
            </ul>
    
            <p><strong>Key Words:</strong> <?php echo $j['keywords']; ?></p>
          </div>
          <?php endforeach; ?>
    
    
        </div>
        <div class="clearfix"></div>
      </div>
    </div>
    <script type="text/javascript">
            jQuery(document).ready(function($) {
                    $('.accordion').accordion({
                            defaultOpen: 'nav-section1',
                            cookieName: 'accordion_nav'
                    });
            });
    
            </script>
    </body>
    </html>
    

    Lib Link: https://github.com/crosbymichael/php-csv-to-xml-json

    i have tried using "\r\n" but it doesnt give the formating either