GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - battye/php-array-parser: Parse text representation of a PHP array into an actual PHP array.

BranchesTags
Open more actions menu

Latest commit

 

History

18 Commits
18 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
composer.lock
composer.lock
 
 
phpunit.xml
phpunit.xml
 
 

Repository files navigation

PHP Array Parser

A small library to parse text representations of a PHP array and return an actual PHP array.

Installation

Run composer install to run this script (and tests) in a standalone way. Alternatively, this can be used as a dependency in another project by running composer require battye/php-array-parser "~1.0".

Reference the namespace at the top of your PHP files to utilise the included classes:

use battye\array_parser\parser;
use battye\array_parser\tokens;

If you notice any bugs, please raise an issue or pull request.

Example

In both of the following examples, $result would contain a PHP array containing the representation of the string or text file provided.

Raw String

To parse a simple array is very easy:

$value = "array(0 => array('one' => 1, 'two' => 'two'));";
$result = parser::parse_simple($value);

In this case, $result would produce the following:

array(1) {
  [0] =>
  array(2) {
    'one' =>
    int(1)
    'two' =>
    string(3) "two"
  }
}

Regex

Regular expressions can also be used to parse complex files and extract array values:

$regex = '/\$lang\s+=\s+array_merge\(\$lang, array\((.*?)\)\);/s';
$file = __DIR__ . '/files/test_lang.php';
$result = parser::parse_regex($regex, $file);

Tests

Latest Stable Version Total Downloads GitHub Actions CI

The unit tests provide good examples of how to utilise this library and can be found in the tests/ directory. To execute the unit tests, run:

vendor/bin/simple-phpunit tests/

About

Parse text representation of a PHP array into an actual PHP array.

Topics

arrayparserphp

Resources

Readme
Activity

Stars

4 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.