我希望将逗号分隔的值字符串解析为数组。我想试试str_getcsv()的php函数,但我找不到任何关于如何使用它的好例子。
例如,我有一个输入,其中用户提交编程语言(php、js、jquery等)的标签,就像提交问题时stackoverflow中的“tag”输入一样。
如何使用str_getcsv将示例value="php, js, jquery"的输入转换为数组?
发布于 2018-03-24 06:05:25
代码非常简单,使用Str_getcsv函数,我们将遍历与脚本位于同一目录中的CSV文件"images.csv“的每一行。
注意:使用的函数与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);
}https://stackoverflow.com/questions/6169235
复制相似问题