File parsing functions in php

File parsing functions in php

  • How to pass PHP Variables by reference ?
  • How to format Phone Numbers in PHP ?
  • How to use php serialize() and unserialize() Function
  • Implementing callback in PHP
  • PHP | Merging two or more arrays using array_merge()
  • PHP program to print an arithmetic progression series using inbuilt functions
  • How to prevent SQL Injection in PHP ?
  • How to extract the user name from the email ID using PHP ?
  • How to count rows in MySQL table in PHP ?
  • How to parse a CSV File in PHP ?
  • How to generate simple random password from a given string using PHP ?
  • How to upload images in MySQL using PHP PDO ?
  • How to check foreach Loop Key Value in PHP ?
  • How to properly Format a Number With Leading Zeros in PHP ?
  • How to get a File Extension in PHP ?

PHP Date Based

  • How to get the current Date and Time in PHP ?
  • PHP program to change date format
  • How to convert DateTime to String using PHP ?
  • How to get Time Difference in Minutes in PHP ?
  • Return all dates between two dates in an array in PHP
  • Sort an array of dates in PHP
  • How to get the time of the last modification of the current page in PHP?
  • How to convert a Date into Timestamp using PHP ?
  • How to add 24 hours to a unix timestamp in php?
  • Sort a multidimensional array by date element in PHP
  • Convert timestamp to readable date/time in PHP
  • PHP | Number of week days between two dates
  • PHP | Converting string to Date and DateTime
  • How to get last day of a month from date in PHP ?
Читайте также:  Get all files ftp php

PHP String Based

  • PHP | Change strings in an array to uppercase
  • How to convert first character of all the words uppercase using PHP ?
  • How to get the last character of a string in PHP ?
  • How to convert uppercase string to lowercase using PHP ?
  • How to extract Numbers From a String in PHP ?
  • How to replace String in PHP ?
  • How to Encrypt and Decrypt a PHP String ?
  • How to display string values within a table using PHP ?
  • How to write Multi-Line Strings in PHP ?
  • How to check if a String Contains a Substring in PHP ?
  • How to append a string in PHP ?
  • How to remove white spaces only beginning/end of a string using PHP ?
  • How to Remove Special Character from String in PHP ?
  • How to create a string by joining the array elements using PHP ?
  • How to prepend a string in PHP ?

PHP Class Based

PHP JSON Based

PHP File Systems Based

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

PHP composer package for parsing data contained in a file into a php array.

nackjicholson/file-parser

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Читайте также:  Java file systems example

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This is a composer package for parsing files that contain structured data.

The current supported formats are:

WARNING Only works with PHP 5.4 and up due to short array syntax and use of traits.

 require_once '../vendor/autoload.php'; $fileParser = new \Nack\FileParser\FileParser(); print_r($fileParser->yaml('foobar.yml'));

compser require nackjicholson/file-parser=~2.1

"require": < "nackjicholson/file-parser": "~2.1" >

This library provides three ways to parse a csv file into a php array. There is full support for delimiter, enclosure, and escape options by passing an associative array of options to each csv method. Options default to:

['delimiter' => ',', 'enclosure' => '"', 'escape' => '\\'] 

There is an example of how to set a file to parse with a ; delimiter in example/example.php .

::csv(mixed $file, array $options = [])

This method provides a literal parse of a file as a csv. Each line is translated to an array of values. Empty lines are not skipped.

foo,bar ,empty first value bingo,bango,bongo 
[ [ 'foo', 'bar' ], [ '', 'empty first value' ], [ null ], [ 'bingo', 'bango', 'bongo' ] ] 

::csvColumnar(mixed $file, array $options = [])

Parses the contents of a csv as data structured columnar. Takes into account the first row of a csv file as column headers, and attaches each column header to its associated row value.

For example, a csv describing a table of contacts.

name,email,phone will,willieviseoae@gmail.com,555-2242 bill,, ,, 

The first row is the headers of the table ‘name’, ’email’, and ‘phone’. The second row is a complete set of data. The third has a name, but empty email and phone. The fourth is not a row, it’s a blank line.

[ [ 'name' => 'will', 'email' => 'willieviseoae@gmail.com', 'phone' => '555-2242' ], [ 'name' => 'bill', 'email' => '', 'phone' => '' ] ] 

::csvRows(mixed $file, array $options = [])

Parses the contents of a csv where each row uses the first value as a key, which is set with the subsequent values. This is ideal for a csv which describes a set of key => value pairs, or key => [ values. ] .

foo,bar bingo,bango,bongo ,, ,nope,not,a,chance emptyValue, 
[ 'foo' => 'bar', 'bingo' => [ 'bango', 'bongo' ], 'emptyValue' => '' ] 

As you can see it ignores blank lines, or lines where the key would be empty.

This method will parse a php ini configuration file into an array. It delegates directly to PHP’s built in function parse_ini_file .

Parses a json file into a php array. This parsing strategy delegates directly to PHP’s built in json_decode .

Parses a yaml file into a php array. This parsing strategy delegates directly to symfony/Yaml

SplFileInfo and SplFileObject Support

Instead of passing a path to any of the file-parser methods. You can supply either a SplFileInfo or SplFileObject object.

$fileParser = new FileParser(); $splFileInfo = new \SplFileInfo('path/to/file.yml'); $fileParser->yaml($splFileInfo); $splFileObject = new \SplFileObject('path/to/file.json'); $fileParser->json($splFileObject);

Report issues, and feel free to make requests there. Tag github issues with the best label you can. If this library doesn’t do something you want, it’s not difficult to extend. The library is built on the strategy pattern, new strategies can parse data differently. Write tests, and make pull requests. If you do not test your code with 100% coverage, your PR will be rejected.

Источник

Parse a CSV File in PHP

Parse a CSV File in PHP

A CSV is one type of file. It means Comma Separated Values . It’s commonly used in different software such as Excel. This allows data to be saved in a tabular format and with a .csv extension.

To parse a CSV file in PHP, it can be done using the combination of fopen() , fgetcsv() and fclose() functions that are built-in in PHP.

/* //sampleFile.csv dog, cat, rat, fish horse, cow, carabao bird, rabbit, chicken */ $ctr = 1; if (($file = fopen("sampleFile.csv", "r")) !== FALSE)   while (($record = fgetcsv($handle, 1000, ",")) !== FALSE)   $total = count($record);  echo " $total fields in line $row: 

\n";
$ctr++; for ($x = 0; $x $ctr; $x++) echo $record[$x] . "
\n";
> > fclose($file); > //output: /* 4 fields in line 1: dog cat 3 fields in line 2: horse cow carabao 3 fields in line 3: bird rabbit chicken */

The example above parses the content of sampleFile.csv using these functions:

fopen(Filename, Mode, Include_path, context) 

This function accepts a URL or file, binds a named resource that is specified by filename to a stream.

  • Filename (required) — This parameter specifies the file or URL to open
  • Mode (required) — This represents the type of access you require to the file.
  • Include_path (optional) — Setting this parameter to 1 will search for the file in the include_path .
  • context (optional) — This specifies the content of the file handle. It’s a set of options that can be updated on the behavior of the stream.
fgetcsv(File, Length, Separator, Enclosure, Escape) 

This function is responsible for parsing a line from an opened CSV file.

  • File (required) — This parameter specifies the open file to return and parse a line from.
  • Length (optional on PHP 5 and below but required to PHP 5.1 and above) — This parameter is the maximum length of the line. This must be greater than the longest line in the file. If this parameter is not present, then the length is not limited, which makes it slower to parse.
  • Separator (optional) — This parameter is the field separator, and the default value is a comma — , .
  • Enclosure (optional) — This parameter is the field enclosure, and the default value is » .
  • Escape (optional) — This parameter is the escape character, and the default is \\ .

This function closes an open file pointer.

Источник

Оцените статью