php str_getcsv 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6169235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
str_getcsv example
提问by CyberJunkie
I want to parse a comma separated value string into an array. I want to try the str_getcsv()
php function but I can't find any good examples on how to use it.
我想将逗号分隔的值字符串解析为数组。我想尝试str_getcsv()
php 函数,但我找不到任何关于如何使用它的好例子。
For example I have an input where users submit tags for programming languages (php, js, jquery, etc), like the "tags" input in stackoverflow when you submit a question.
例如,我有一个输入,用户可以在其中提交编程语言(php、js、jquery 等)的标签,例如当您提交问题时,stackoverflow 中的“标签”输入。
How would I turn an input with example value="php, js, jquery"
into an array using str_getcsv?
如何value="php, js, jquery"
使用 str_getcsv将带有示例的输入转换为数组?
采纳答案by Femi
Its true that the spec at http://us3.php.net/manual/en/function.str-getcsv.phpdoesn't include a standard example, but the user-submitted notes do a decent job of covering it. If this is a form input:
http://us3.php.net/manual/en/function.str-getcsv.php上的规范确实没有包含标准示例,但是用户提交的注释在涵盖它方面做得很好。如果这是表单输入:
$val = $_POST['value'];
$data = str_getcsv($val);
$data
is now an array with the values. Try it and see if you have any other issues.
$data
现在是一个包含值的数组。尝试一下,看看您是否还有其他问题。
回答by stefgosselin
I think you should maybe look at explodefor this task.
我认为你应该看看这个任务的爆炸。
$values = "php, js, jquery";
$items = explode(",", $values);
// Would give you an array:
echo $items[0]; // would be php
echo $items[1]; // would be js
echo $items[2]; // would be jquery
This would probably more efficient than str_getcsv();
这可能比 str_getcsv() 更有效;
Note that you would need to use trim() to remove possible whitespace befores and after item values.
请注意,您需要使用 trim() 删除项目值前后可能的空格。
UPDATE:
更新:
I hadn't seen str_getcsv before, but read this quote on the manpage that would make it seem a worthwhile candidate:
我之前没有见过 str_getcsv,但是在手册页上阅读了这句话,这将使它看起来是一个值得的候选人:
Why not use explode() instead of str_getcsv() to parse rows? Because explode() would not treat possible enclosured parts of string or escaped characters correctly.
为什么不使用explode() 而不是str_getcsv() 来解析行?因为explode() 不会正确处理字符串的可能封闭部分或转义字符。
回答by user336828
For simplicity and readability, I typically find myself using just explode(), only adding in str_getcsv() if the following two conditions are met: 1) the primary delimiter is also used within the data itself; 2) the token that I'm trying to use as the main delimiter is enclosed by another distinct character.
为了简单和可读性,我通常只使用explode(),只有在满足以下两个条件时才添加str_getcsv():1) 主分隔符也用于数据本身;2) 我试图用作主要分隔符的标记被另一个不同的字符包围。
For example, a basic parser for a CSV file:
例如,CSV 文件的基本解析器:
$filename = $argv[1];
if (empty($filename)) { echo "Input file required\n"; exit; }
$AccountsArray = explode("\n", file_get_contents($filename));
As long as each of the elements of $AccountsArray doesn't embed a "," within the data itself, this will work perfectly and is straightforward and easy to follow:
只要 $AccountsArray 的每个元素没有在数据本身中嵌入“,”,这将完美地工作并且简单易懂:
foreach ($AccountsArray as $entry) {
$acctArr = explode(",", $entry);
}
However, often the data will contain the delimiter, at which point an enclosing token (a " in this example) has to be present. If so, then I switch to str_getcsv() like so:
但是,通常数据将包含分隔符,此时必须存在一个封闭标记(在本例中为“)。如果是这样,那么我切换到 str_getcsv() 像这样:
foreach ($AccountsArray as $entry) {
$acctArr = str_getcsv($entry, ",", "\"");
}
回答by user336828
//get the csv
$csvFile = file_get_contents('test.csv');
//separate each line
$csv = explode("\n",$csvFile);
foreach ($csv as $csvLine) {
//separet each fields
$linkToInsert = explode(",",$csvLine);
//echo what you need
//$linkToInsert[0] would be the first field, $linkToInsert[1] second field, etc
echo '? ' . $linkToInsert[1] . '<br>';
}
回答by Anderson Le?o Gimenes
The code is quite simple, using the Str_getcsv
function, we will go
through each line of the CSV file "images.csv" that is located in the
same directory as our script.
代码非常简单,使用该Str_getcsv
函数,我们将遍历与我们的脚本位于同一目录中的 CSV 文件“images.csv”的每一行。
NOTE: Functions used are compatible with versions of PHP >= 5.3.0
注意:使用的函数与 PHP >= 5.3.0 版本兼容
//First, reading the CSV file
$csvFile = file('file.csv');
foreach ($csvFile as $line) {
$url = str_getcsv($line);
$ch = curl_init($url[0]);
$name = basename($url[0]);
if (!file_exists('directory/' . $name)) {
$fp = fopen('directory/' . $name, 'wb');
}
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}