PHP 7 Multi-format Streaming Parser

PHP 7 Multi-format Streaming Parser

PHP 7 Stream Parser is a package by Sergio Ródenas for PHP multi-format stream parsing. Using streams with a pull parser is more efficient in large documents than DOM loading.

Here’s an example of how you might use the parser with a Laravel queue:

StreamParser::xml("")->each(function(Collection $user){
    dispatch(new App\Jobs\SendEmail($user));

Given the following XML data example, it becomes clear how this stream parser works with Collections:

    <book ISBN="10-000000-001">
        <title>The Iliad and The Odyssey</title>
            <userComment rating="4">
                Best translation I've read.
            <userComment rating="2">
                I like other versions better.

And an example of the PHP code and output:

StreamParser::xml("")->each(function(Collection $book){

// Output
class Illuminate\Support\Collection#19 (1) {
  protected $items =>
  array(4) {
    'ISBN' =>
    string(13) "10-000000-001"
    'title' =>
    string(25) "The Iliad and The Odyssey"
    'price' =>
    string(5) "12.95"
    'comments' =>
    class Illuminate\Support\Collection#17 (1) {
      protected $items =>
      array(2) {
array(2) {
  [0] =>
  array(2) {
    'rating' =>
    string(1) "4"
    'userComment' =>
    string(27) "Best translation I've read."
  [1] =>
  array(2) {
    'rating' =>
    string(1) "2"
    'userComment' =>
    string(29) "I like other versions better."

Here’s the same example using CSV data:

The Iliad and The Odyssey,12.95,"Best translation I've read.,I like other versions better."
Anthology of World Literature,24.95,"Needs more modern literature.,Excellent overview of world literature."

And an example of the stream parser code:

StreamParser::csv("")->each(function(Collection $book){

// Output
string(29) "I like other versions better."
string(39) "Excellent overview of world literature."

Learn More

You can learn more about this package on GitHub and install it with composer require Rodenastyle/stream-parser. The README also mentions this article, which explains processing XML files via DOM vs. Streaming.

Filed in: News


Join 31,000+ others and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Laravel Developer
FPV Exchange
Senior Full-Stack Developer
Remote, USA Only
Full Stack Web Developer
Greenville, TX (or Remote, US)
Innovation First International, Inc.
Senior Software Developer
UK/Germany Only
The Greenhouse People Ltd
Senior PHP Developer
Cary, NC