PHP: SplFileObject::getCsvControl - Manual (original) (raw)
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
SplFileObject::getCsvControl — Get the delimiter, enclosure and escape character for CSV
Description
public SplFileObject::getCsvControl(): array
Parameters
This function has no parameters.
Return Values
Returns an indexed array containing the delimiter, enclosure and escape character.
Changelog
| Version | Description |
|---|---|
| 7.4.0 | The escape character can now be an empty string. |
| 7.0.10 | Added the escape character to the returned array. |
Examples
Example #1 SplFileObject::getCsvControl() example
<?php $file = new SplFileObject("data.txt"); print_r($file->getCsvControl()); ?>
The above example will output something similar to:
Array
(
[0] => ,
[1] => "
[2] =>
)
See Also
- SplFileObject::setCsvControl() - Set the delimiter, enclosure and escape character for CSV
- SplFileObject::fgetcsv() - Gets line from file and parse as CSV fields
- SplFileObject::fputcsv() - Write a field array as a CSV line
- fputcsv() - Format line as CSV and write to file pointer
- fgetcsv() - Gets line from file pointer and parse for CSV fields
- str_getcsv() - Parse a CSV string into an array
Found A Problem?
greg dot bowler at g105b dot com ¶
10 years ago
Note that this function does not magically guess the CSV control from a given file, rather it returns what has been priorly set with SplFileObject::setCsvControl().faure dot daniel dot 57 at gmail dot com ¶
4 years ago
Given an absolute path to a CSV or any text file and a list of possible delimiters and assuming lines are up to 4096 characters long, I use
<?php
function guess_delimiter($file, $delimiters=[',',';'])
{
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi><mo>=</mo><mi>f</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>n</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">h = fopen(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">h</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">o</span><span class="mord mathnormal">p</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mopen">(</span></span></span></span>file,'r');
$count = [];
foreach ($delimiters as $del) {
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>t</mi><mo stretchy="false">[</mo></mrow><annotation encoding="application/x-tex">count[</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mopen">[</span></span></span></span>del] = 0;
while (($bufer = fgets($h, 4096)) !== false) {
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>t</mi><mo stretchy="false">[</mo></mrow><annotation encoding="application/x-tex">count[</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mopen">[</span></span></span></span>del]+=substr_count($bufer, $del);
}
rewind($h);
}
fclose($h);
return array_search(max($count), $count);
}
12 years ago
Seems that this function always returns the same delimiter.
<?php
file_put_contents("A;B;C;D\n0;0;0;0", "test.txt");
$file = new SplFileObject("test.txt");
var_dump($file->getCsvControl());
?>
array(2) {
[0]=>
string(1) ","
[1]=>
string(1) """
}