當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。