开发者

str_getcsv example

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.

For example I have an input wh开发者_C百科ere users submit tags for programming languages (php, js, jquery, etc), like the "tags" input in stackoverflow when you submit a question.

How would I turn an input with example value="php, js, jquery" into an array using str_getcsv?


Its true that the spec at http://us3.php.net/manual/en/function.str-getcsv.php doesn't include a standard example, but the user-submitted notes do a decent job of covering it. If this is a form input:

$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.


I think you should maybe look at explode for 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();

Note that you would need to use trim() to remove possible whitespace befores and after item values.

UPDATE:

I hadn't seen str_getcsv before, but read this quote on the manpage that would make it seem a worthwhile candidate:

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.


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.

For example, a basic parser for a CSV file:

$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:

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:

foreach ($AccountsArray as $entry) {
    $acctArr = str_getcsv($entry, ",", "\""); 
}


//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>';
}


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.

NOTE: Functions used are compatible with versions of 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);         
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜