function processCsv($file, $omitTitles = true)
{
    $csv = new SplFileObject($file);
    $records = 0;
    if ($omitTitles) {
        // Get the first line but do nothing with it
        $csv->getCurrentLine();
    }
    while (!$csv->eof()) {
        // Get the current line
        $line = $csv->fgetcsv();
        // Skip blank lines that return a single null element
        if (count($line) > 1 && !is_null($line[0])) {
            (yield $line);
            $records++;
        }
    }
    return "{$records} records processed from {$file}";
}
<?php

$file = new SplFileObject('names.csv');
// Discard the column headers
$file->getCurrentLine();
// Build a multidimensional array of the remaining lines
while (!$file->eof()) {
    $line = $file->fgetcsv();
    // Ignore empty lines
    if (!is_null($line[0])) {
        $names[] = $line;
    }
}
/*
 * sort function, if first is bigger than second return -1, else return 1, or 0 if it's equals
 */
usort($names, function ($a, $b) {
    return [$a[1], $a[0]] <=> [$b[1], $b[0]];
});
// Display the names
foreach ($names as $name) {
    echo implode(' ', $name) . '<br>';
}
Esempio n. 3
0
/**
 * @author  Atanas Vasilev
 * @link    http://pastebin.com/dHbqjUNy
 * @see     http://www.dotvoid.com/2010/04/detecting-utf-bom-byte-order-mark/
 * @version 1.1
 */
// SETTINGS
////////////////////////////////////////////////////////////////////////////////
$check_extensions = array('php');
// MAIN
////////////////////////////////////////////////////////////////////////////////
define('STR_BOM', "");
$file = null;
$directory = getcwd();
$rit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::CHILD_FIRST);
echo '<h1>BOM Check</h1>';
try {
    foreach ($rit as $file) {
        if ($file->isFile()) {
            $path_parts = pathinfo($file->getRealPath());
            if (isset($path_parts['extension']) && in_array($path_parts['extension'], $check_extensions)) {
                $object = new SplFileObject($file->getRealPath());
                if (false !== strpos($object->getCurrentLine(), STR_BOM)) {
                    echo $file->getRealPath() . '<br />';
                }
            }
        }
    }
} catch (Exception $e) {
    die('Exception caught: ' . $e->getMessage());
}
<?php

//line 2
//line 3
//line 4
//line 5
$s = new SplFileObject(__FILE__);
$s->seek(1);
echo $s->getCurrentLine();
echo $s->getCurrentLine();
Esempio n. 5
0
<?php

$splFileObject = new SplFileObject(__FILE__);
$splFileObject->setMaxLineLen(3);
$line = $splFileObject->getCurrentLine();
var_dump($line === '<?p');
var_dump(strlen($line) === 3);
Esempio n. 6
0
$str = 'Container';
$file = null;
$directory = getcwd();
$directory = './';
echo '<xmp>';
$rit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::CHILD_FIRST);
try {
    $count = 0;
    foreach ($rit as $file) {
        //print 'processing ' . $file->getRealPath()."\n";
        if ($file->isFile()) {
            $path_parts = pathinfo($file->getRealPath());
            if ('php' == $path_parts['extension']) {
                $object = new SplFileObject($file->getRealPath());
                while (!$object->eof()) {
                    $line = $object->getCurrentLine();
                    if (false !== ($pos = strpos($line, $str))) {
                        $count++;
                        print $file->getRealPath() . "\n";
                        print $line;
                        print substr($line, $pos);
                        break;
                    }
                }
            }
        } else {
            //print 'processing ' . $file->getRealPath()."\n";
            flush();
        }
    }
    echo 'done ' . $count . ' files found with ' . $str;
 /**
  * @return \Mathielen\ImportEngine\Storage\Format\Format
  */
 public function factor($uri)
 {
     $file = new \SplFileObject($uri);
     $delimiter = $this->guessDelimiter(utf8_encode($file->getCurrentLine()));
     return new CsvFormat($delimiter);
 }