{"id":17492131,"url":"https://github.com/jupitern/file-parser","last_synced_at":"2025-04-22T20:16:06.687Z","repository":{"id":57003147,"uuid":"81564805","full_name":"jupitern/file-parser","owner":"jupitern","description":"a fluent way to read, filter, parse and format {csv, tsv, dsv, variable-length-delimited} and other txt files","archived":false,"fork":false,"pushed_at":"2024-12-04T12:39:23.000Z","size":15,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T20:16:01.241Z","etag":null,"topics":["csv","dsv","parser","php","php7","txt","variable-length-delimited"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jupitern.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-10T12:49:47.000Z","updated_at":"2024-12-04T12:38:48.000Z","dependencies_parsed_at":"2022-08-21T12:10:49.866Z","dependency_job_id":null,"html_url":"https://github.com/jupitern/file-parser","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupitern%2Ffile-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupitern%2Ffile-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupitern%2Ffile-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupitern%2Ffile-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jupitern","download_url":"https://codeload.github.com/jupitern/file-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250316066,"owners_count":21410476,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","dsv","parser","php","php7","txt","variable-length-delimited"],"created_at":"2024-10-19T08:08:02.895Z","updated_at":"2025-04-22T20:16:06.667Z","avatar_url":"https://github.com/jupitern.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# jupitern/file-parser\n#### PHP File Parser.\n\nread, filter, parse and format {csv, tsv, dsv, variable-length-delimited} from files or strings\n\n## Requirements\n\nPHP 8.0 or higher.\n\n## Installation\n\nInclude jupitern/file-parser in your project, by adding it to your composer.json file.\n```javascript\n{\n    \"require\": {\n        \"jupitern/file-parser\": \"1.*\"\n    }\n}\n```\n\n## Usage\n```php\n\nLets parse a csv from a string with contents (animal, category, count):\nanimal,type,count\ncrocodile,reptile,4\ndolphin,mammal,0\nduck,bird,2\nkoala,mammal,4\nlion,mammal,5\n\nlets parse the file with:\n    - ignore the first line\n    - convert encoding from ISO-9959-1 to UTF-8\n    - convert lines to objects\n    - remove animals with count 0\n    - format the animal type to uppercase\n    - group by type\n\n$objectsArr = \\Jupitern\\Parser\\FileParser::instance()\n            -\u003efromString('animal,type,count\ncrocodile,reptile,4\ndolphin,mammal,0\nduck,bird,2\nkoala,mammal,4\nlion,mammal,5', ',')\n            -\u003esetEncoding('ISO-8859-1', 'UTF-8')\n            -\u003etoObject(['animal', 'type', 'animalCount'])\n            -\u003efilter(function ($line, $lineNumber) {\n                return $lineNumber \u003e 1 \u0026\u0026 $line-\u003eanimalCount \u003e 0;\n            })\n            -\u003eformat('type', function ($val) {\n                return strtoupper($val);\n            })\n            -\u003egroup(function ($line) {\n                return $line-\u003etype;\n            })\n            -\u003eparse();\n\n        echo '\u003cpre\u003e';\n        print_r($objectsArr);\n\n/*\noutput:\nArray\n(\n    [REPTILE] =\u003e Array\n        (\n            [0] =\u003e stdClass Object\n                (\n                    [animal] =\u003e crocodile\n                    [type] =\u003e REPTILE\n                    [animalCount] =\u003e 4\n                )\n\n        )\n\n    [BIRD] =\u003e Array\n        (\n            [0] =\u003e stdClass Object\n                (\n                    [animal] =\u003e duck\n                    [type] =\u003e BIRD\n                    [animalCount] =\u003e 2\n                )\n\n        )\n\n    [MAMMAL] =\u003e Array\n        (\n            [0] =\u003e stdClass Object\n                (\n                    [animal] =\u003e koala\n                    [type] =\u003e MAMMAL\n                    [animalCount] =\u003e 4\n                )\n\n            [1] =\u003e stdClass Object\n                (\n                    [animal] =\u003e lion\n                    [type] =\u003e MAMMAL\n                    [animalCount] =\u003e 5\n                )\n\n        )\n\n)\n*/\n\nGiven a csv file \"filename.csv\" with contents (animal, category, count):\nanimal,type,count\ncrocodile,reptile,4\ndolphin,mammal,0\nduck,bird,2\nkoala,mammal,4\nlion,mammal,5\n\nlets parse the file with:\n    - ignore the first line\n    - convert encoding from ISO-9959-1 to UTF-8\n    - convert lines to objects\n    - remove animals with count 0\n    - format the animal type to uppercase\n    - group by type\n\n// read a file to array\n$objectsArr = \\Jupitern\\Parser\\FileParser::instance()\n    -\u003efromFile('D:\\\\aaa.txt', ',')\n    -\u003esetEncoding('ISO-8859-1', 'UTF-8')\n    -\u003etoObject(['animal', 'type', 'animalCount'])\n    -\u003efilter(function ($line, $lineNumber) {\n        return $lineNumber \u003e 1 \u0026\u0026 $line-\u003eanimalCount \u003e 0;\n    })\n    -\u003eformat('type', function ($val) {\n        return strtoupper($val);\n    })\n    -\u003egroup(function ($line) {\n        return $line-\u003etype;\n    })\n    -\u003eparse();\n\necho '\u003cpre\u003e';\nprint_r($objectsArr);\n\n/*\noutput:\nArray\n(\n    [REPTILE] =\u003e Array\n        (\n            [0] =\u003e stdClass Object\n                (\n                    [animal] =\u003e crocodile\n                    [type] =\u003e REPTILE\n                    [animalCount] =\u003e 4\n                )\n\n        )\n\n    [BIRD] =\u003e Array\n        (\n            [0] =\u003e stdClass Object\n                (\n                    [animal] =\u003e duck\n                    [type] =\u003e BIRD\n                    [animalCount] =\u003e 2\n                )\n\n        )\n\n    [MAMMAL] =\u003e Array\n        (\n            [0] =\u003e stdClass Object\n                (\n                    [animal] =\u003e koala\n                    [type] =\u003e MAMMAL\n                    [animalCount] =\u003e 4\n                )\n\n            [1] =\u003e stdClass Object\n                (\n                    [animal] =\u003e lion\n                    [type] =\u003e MAMMAL\n                    [animalCount] =\u003e 5\n                )\n\n        )\n\n)\n*/\n\n\nFor the same file lets parse with:\n   - convert encoding from ISO-9959-1 to UTF-8\n   - convert lines to arrays\n   - remove animals with count 0\n   - group by type\n\n$objectsArr = \\Jupitern\\Parser\\FileParser::instance()\n    -\u003esetFile(\"csv.txt\", ',')\n    -\u003esetEncoding('ISO-8859-1', 'UTF-8')\n    -\u003efilter(function ($line, $lineNumber) {\n        return $lineNumber \u003e 1 \u0026\u0026 $line[2] \u003e 0;\n    })\n    -\u003egroup(function ($line) {\n        return $line[1];\n    })\n    -\u003eparse();\n\necho '\u003cpre\u003e';\nprint_r($objectsArr);\n\n/*\nOutput:\nArray\n(\n    [reptile] =\u003e Array\n        (\n            [0] =\u003e Array\n                (\n                    [0] =\u003e crocodile\n                    [1] =\u003e reptile\n                    [2] =\u003e 4\n                )\n\n        )\n\n    [bird] =\u003e Array\n        (\n            [0] =\u003e Array\n                (\n                    [0] =\u003e duck\n                    [1] =\u003e bird\n                    [2] =\u003e 2\n                )\n\n        )\n\n    [mammal] =\u003e Array\n        (\n            [0] =\u003e Array\n                (\n                    [0] =\u003e koala\n                    [1] =\u003e mammal\n                    [2] =\u003e 4\n                )\n\n            [1] =\u003e Array\n                (\n                    [0] =\u003e lion\n                    [1] =\u003e mammal\n                    [2] =\u003e 5\n                )\n\n        )\n\n)\n*/\n\n\nGiven a dsv file \"file.txt\" with contents (empolyee number, birth date, monthly income):\n01john doe        1980-01-01          923.5\n01luis west       1976-01-01         1143.3\n01madalena        1983-01-01         2173.6\n02Jaqueline Wayne 1983-01-01         822.44\n05luís manuel     1983-01-01        1323.52\n\nlets parse the file doing:\n    - convert encoding from ISO-9959-1 to UTF-8\n    - convert lines to objects\n    - format person name capitalize first letters\n    - group by wage bellow or above 1000\n\n$objectsArr = \\Jupitern\\Parser\\FileParser::instance()\n    -\u003esetFile(\"test.txt\")\n    -\u003esetEncoding('ISO-8859-1', 'UTF-8')\n    -\u003eeach(function ($line){\n        $obj = [];\n        $obj['Number'] = mb_substr($line, 0, 2);\n        $obj['Name'] = mb_substr($line, 2, 16);\n        $obj['BirthDate'] = mb_substr($line, 18, 10);\n        $obj['MonthlyIncome'] = (float)mb_substr($line, 28, 15);\n        return (object)$obj;\n    })\n    -\u003eformat('Name', function ($val) {\n        return ucwords($val);\n    })\n    -\u003egroup(function ($line) {\n        return (float)$line-\u003eMonthlyIncome \u003e= 1000 ? 'above 1000' : 'bellow 1000';\n    })\n    -\u003eparse();\n\necho '\u003cpre\u003e';\nprint_r($objectsArr);\n\n/*\nOutput:\nArray\n(\n    [bellow 1000] =\u003e Array\n        (\n            [0] =\u003e stdClass Object\n                (\n                    [Number] =\u003e 01\n                    [Name] =\u003e John Doe\n                    [BirthDate] =\u003e 1980-01-01\n                    [MonthlyIncome] =\u003e 923.5\n                )\n\n            [1] =\u003e stdClass Object\n                (\n                    [Number] =\u003e 02\n                    [Name] =\u003e Jaqueline Wayne\n                    [BirthDate] =\u003e 1983-01-01\n                    [MonthlyIncome] =\u003e 822.44\n                )\n\n            [2] =\u003e stdClass Object\n                (\n                    [Number] =\u003e 05\n                    [Name] =\u003e LuÃ­s Manuel\n                    [BirthDate] =\u003e  1983-01-0\n                    [MonthlyIncome] =\u003e 1\n                )\n\n        )\n\n    [above 1000] =\u003e Array\n        (\n            [0] =\u003e stdClass Object\n                (\n                    [Number] =\u003e 01\n                    [Name] =\u003e Luis West\n                    [BirthDate] =\u003e 1976-01-01\n                    [MonthlyIncome] =\u003e 1143.3\n                )\n\n            [1] =\u003e stdClass Object\n                (\n                    [Number] =\u003e 01\n                    [Name] =\u003e Madalena\n                    [BirthDate] =\u003e 1983-01-01\n                    [MonthlyIncome] =\u003e 2173.6\n                )\n\n        )\n\n)\n*/\n\n```\n\n## ChangeLog\n\nv1.2.0\n\n- min php version updated to 8.0\n- code refactor for php8\n- allow parse from string or file\n\nv1\n - initial release\n\n## Contributing\n\n - welcome to discuss a bugs, features and ideas.\n\n## License\n\njupitern/file-parser is release under the MIT license.\n\nYou are free to use, modify and distribute this software\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjupitern%2Ffile-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjupitern%2Ffile-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjupitern%2Ffile-parser/lists"}