gpt4 book ai didi

php - CSV 新行字符

转载 作者:可可西里 更新时间:2023-11-01 00:14:49 25 4
gpt4 key购买 nike

我有一个 Excel 模板,我用它来填写数据并将其导出为 CSV 以填充我网站上的以下页面。

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

当我在 Windows 中导出文件时,我得到了新行字符的正确格式。但是当我从 MAC 导出相同的 excel 文件时,我没有得到换行符。

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

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

.

我的代码

<?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 jobs@ifocussystec.com</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>

图书馆链接:https://github.com/crosbymichael/php-csv-to-xml-json

我试过使用 "\r\n" 但它也没有给出格式

最佳答案

PHP 文件函数不能可靠地识别来自 Mac 的行结尾(如果它们使用 \r 字符,就像 Mac Excel 那样,如果我没记错的话。)

你需要设置auto_detect_line_endings在 php.ini 中设置为 true,那么它将正确识别行尾,无论它们是 \n\r 还是 \r\n .

这个设置是 PHP_INI_ALL,这意味着它可以在任何地方设置,即在 php.ini 中,在特定目录的 .htaccess 中,甚至用 ini_set ,例如,ini_set('auto_detect_line_endings', true)

关于php - CSV 新行字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16520282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com