mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65566adcd3 | ||
|
|
2d428ffa93 | ||
|
|
bc9207de09 | ||
|
|
ace09c3c11 | ||
|
|
52ad56c66a | ||
|
|
ab9e8a0af9 | ||
|
|
8aa61914f7 | ||
|
|
3b74f7ce57 |
@@ -113,6 +113,7 @@ print_r($csv->data_types);
|
|||||||
|
|
||||||
**Modify data in a CSV file**
|
**Modify data in a CSV file**
|
||||||
|
|
||||||
|
Change data values:
|
||||||
```php
|
```php
|
||||||
$csv = new ParseCsv\Csv();
|
$csv = new ParseCsv\Csv();
|
||||||
$csv->sort_by = 'id';
|
$csv->sort_by = 'id';
|
||||||
@@ -122,6 +123,14 @@ $csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'jo
|
|||||||
$csv->save();
|
$csv->save();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Enclose each data value by quotes:
|
||||||
|
```php
|
||||||
|
$csv = new ParseCsv\Csv();
|
||||||
|
$csv->parse('data.csv');
|
||||||
|
$csv->enclose_all = true;
|
||||||
|
$csv->save();
|
||||||
|
```
|
||||||
|
|
||||||
**Replace field names or set ones if missing**
|
**Replace field names or set ones if missing**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
|||||||
27
examples/save_to_file_without_header_row.php
Normal file
27
examples/save_to_file_without_header_row.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
# include parseCSV class.
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use ParseCsv\Csv;
|
||||||
|
|
||||||
|
|
||||||
|
# Create new parseCSV object.
|
||||||
|
$csv = new Csv();
|
||||||
|
|
||||||
|
# When saving, don't write the header row:
|
||||||
|
$csv->heading = false;
|
||||||
|
|
||||||
|
# Specify which columns to write, and in which order.
|
||||||
|
# We won't output the 'Awesome' column this time.
|
||||||
|
$csv->titles = ['Age', 'Name'];
|
||||||
|
|
||||||
|
# Data to write:
|
||||||
|
$csv->data = [
|
||||||
|
0 => ['Name' => 'Anne', 'Age' => 45, 'Awesome' => true],
|
||||||
|
1 => ['Name' => 'John', 'Age' => 44, 'Awesome' => false],
|
||||||
|
];
|
||||||
|
|
||||||
|
# Then we save the file to the file system:
|
||||||
|
$csv->save('people.csv');
|
||||||
@@ -846,10 +846,10 @@ class Csv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// this is needed because sometime titles property is overwritten instead of using fields parameter!
|
// this is needed because sometime titles property is overwritten instead of using fields parameter!
|
||||||
$titlesOnParse = !empty($this->data) ? array_keys($this->data[0]) : array();
|
$titlesOnParse = !empty($this->data) ? array_keys(reset($this->data)) : array();
|
||||||
|
|
||||||
// both are identical, also in ordering
|
// both are identical, also in ordering OR we have no data (only titles)
|
||||||
if (array_values($fields) === array_values($titlesOnParse)) {
|
if (empty($titlesOnParse) || array_values($fields) === array_values($titlesOnParse)) {
|
||||||
return array_combine($fields, $fields);
|
return array_combine($fields, $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -889,7 +889,7 @@ class Csv {
|
|||||||
|
|
||||||
if (is_null($input)) {
|
if (is_null($input)) {
|
||||||
$file = $this->file;
|
$file = $this->file;
|
||||||
} elseif (file_exists($input)) {
|
} elseif (\strlen($input) <= PHP_MAXPATHLEN && file_exists($input)) {
|
||||||
$file = $input;
|
$file = $input;
|
||||||
} else {
|
} else {
|
||||||
$data = $input;
|
$data = $input;
|
||||||
|
|||||||
@@ -59,7 +59,9 @@ class ConstructTest extends TestCase {
|
|||||||
/** @noinspection PhpIncludeInspection */
|
/** @noinspection PhpIncludeInspection */
|
||||||
require $script_file;
|
require $script_file;
|
||||||
$ob_get_clean = ob_get_clean();
|
$ob_get_clean = ob_get_clean();
|
||||||
if ($script_file != 'download.php') {
|
$verb = strtok($script_file, '_.');
|
||||||
|
|
||||||
|
if (!in_array($verb, ['download', 'save'], true)) {
|
||||||
$this->assertContains('<td>', $ob_get_clean);
|
$this->assertContains('<td>', $ob_get_clean);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,6 +188,11 @@ class ParseTest extends TestCase {
|
|||||||
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testVeryLongNonExistingFile() {
|
||||||
|
$this->csv->parse(str_repeat('long_string', PHP_MAXPATHLEN));
|
||||||
|
$this->csv->auto(str_repeat('long_string', PHP_MAXPATHLEN));
|
||||||
|
}
|
||||||
|
|
||||||
protected function _get_magazines_data() {
|
protected function _get_magazines_data() {
|
||||||
return [
|
return [
|
||||||
[
|
[
|
||||||
|
|||||||
@@ -75,6 +75,19 @@ class UnparseTest extends Testcase {
|
|||||||
$this->unparseAndCompare($expected, $fields);
|
$this->unparseAndCompare($expected, $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefaultFirstRowMissing(){
|
||||||
|
unset($this->csv->data[0]);
|
||||||
|
$expected = "column1,column2\rvalue3,value4\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnparseDefaultWithoutData(){
|
||||||
|
unset($this->csv->data[0]);
|
||||||
|
unset($this->csv->data[1]);
|
||||||
|
$expected = "column1,column2\r";
|
||||||
|
$this->unparseAndCompare($expected);
|
||||||
|
}
|
||||||
|
|
||||||
private function unparseAndCompare($expected, $fields = array()) {
|
private function unparseAndCompare($expected, $fields = array()) {
|
||||||
$str = $this->csv->unparse($this->csv->data, $fields);
|
$str = $this->csv->unparse($this->csv->data, $fields);
|
||||||
$this->assertEquals($expected, $str);
|
$this->assertEquals($expected, $str);
|
||||||
|
|||||||
Reference in New Issue
Block a user