当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP SplFileObject getCsvControl()用法及代码示例


SplFileObject::getCsvControl() 是 PHP 中的内置函数,用于检索 SplFileObject 类实例的当前 CSV 控制参数。这些参数决定如何从文件中解析和处理 CSV 数据。

用法

public SplFileObject::getCsvControl(): array

参数

该函数没有任何参数。

返回值

SplFileObject::getCsvControl() 返回三个元素分隔符、封装和转义字符。

程序1:下面的程序演示了SplFileObject::getCsvControl()函数。

PHP


<?php 
$file = new SplFileObject("./data.txt", "r"); 
$csvControl = $file->getCsvControl(); 
  
// $csvControl is an array  
// containing CSV control parameters 
list($delimiter, $enclosure, $escape) = $csvControl; 
echo "Delimiter: $delimiter\n"; 
echo "Enclosure: $enclosure\n"; 
echo "Escape: $escape\n"; 
?>

输出:

Delimiter: ,
Enclosure: "
Escape: \

程序2:下面的程序演示了SplFileObject::getCsvControl()函数。

PHP


<?php 
$file = new SplFileObject("data.csv", "r"); 
$csvControl = $file->getCsvControl(); 
  
list($delimiter, $enclosure, $escape) = $csvControl; 
  
echo "Original Delimiter: $delimiter\n"; 
echo "Enclosure: $enclosure\n"; 
echo "Escape: $escape\n"; 
  
if ($delimiter !== ",") { 
    
    // Change delimiter to a semicolon 
    $newDelimiter = ";"; 
    $file->setCsvControl($newDelimiter, $enclosure, $escape); 
  
    echo "Delimiter changed to: $newDelimiter\n"; 
} 
  
// Rewind the file to the beginning 
$file->rewind(); 
  
// Read and display the modified CSV data 
foreach ($file as $row) { 
    $data = $file->fgetcsv(); 
    if ($data !== false) { 
        echo implode(", ", $data) . "\n"; 
    } 
} 
  
?>

注意:将此文件名保存为“data.csv

1,"Eldon Base for stackable storage shelf, platinum",Muhammed MacIntyre,3,-213.25,38.94,35,Nunavut,Storage & Organization,0.8
2,"1.7 Cubic Foot Compact ""Cube"" Office Refrigerators",Barry French,293,457.81,208.16,68.02,Nunavut,Appliances,0.58
3,"Cardinal Slant-D� Ring Binder, Heavy Gauge Vinyl",Barry French,293,46.71,8.69,2.99,Nunavut,Binders and Binder Accessories,0.39
4,R380,Clay Rozendal,483,1198.97,195.99,3.99,Nunavut,Telephones and Communication,0.58
5,Holmes HEPA Air Purifier,Carlos Soltero,515,30.94,21.78,5.94,Nunavut,Appliances,0.5
6,G.E. Longer-Life Indoor Recessed Floodlight Bulbs,Carlos Soltero,515,4.43,6.64,4.95,Nunavut,Office Furnishings,0.37
7,"Angle-D Binders with Locking Rings, Label Holders",Carl Jackson,613,-54.04,7.3,7.72,Nunavut,Binders and Binder Accessories,0.38
8,"SAFCO Mobile Desk Side File, Wire Frame",Carl Jackson,613,127.70,42.76,6.22,Nunavut,Storage & Organization,
9,"SAFCO Commercial Wire Shelving, Black",Monica Federle,643,-695.26,138.14,35,Nunavut,Storage & Organization,
10,Xerox 198,Dorothy Badders,678,-226.36,4.98,8.33,Nunavut,Paper,0.38

输出:

Original Delimiter: ,
Enclosure: "
Escape: \
2, 1.7 Cubic Foot Compact "Cube" Office Refrigerators, Barry French, 293, 457.81, 208.16, 68.02, Nunavut, Appliances, 0.58
4, R380, Clay Rozendal, 483, 1198.97, 195.99, 3.99, Nunavut, Telephones and Communication, 0.58
6, G.E. Longer-Life Indoor Recessed Floodlight Bulbs, Carlos Soltero, 515, 4.43, 6.64, 4.95, Nunavut, Office Furnishings, 0.37
8, SAFCO Mobile Desk Side File, Wire Frame, Carl Jackson, 613, 127.70, 42.76, 6.22, Nunavut, Storage & Organization, 
10, Xerox 198, Dorothy Badders, 678, -226.36, 4.98, 8.33, Nunavut, Paper, 0.38

参考:https://www.php.net/manual/en/splfileobject.getcsvcontrol.php



相关用法


注:本文由纯净天空筛选整理自neeraj3304大神的英文原创作品 PHP SplFileObject getCsvControl() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。