Socket
Socket
Sign inDemoInstall

php-parser

Package Overview
Dependencies
0
Maintainers
2
Versions
74
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    php-parser

Parse PHP code from JS and returns its AST


Version published
Weekly downloads
135K
decreased by-0.41%
Maintainers
2
Install size
789 kB
Created
Weekly downloads
 

Readme

Source

php-parser

This JavaScript library parses PHP code and converts it to an AST.

Installation

This library is distributed with npm :

npm install php-parser --save

Usage

// initialize the php parser factory class
const fs = require("fs");
const path = require("path");
const engine = require("php-parser");

// initialize a new parser instance
const parser = new engine({
  // some options :
  parser: {
    extractDoc: true,
    php7: true,
  },
  ast: {
    withPositions: true,
  },
});

// Retrieve the AST from the specified source
const eval = parser.parseEval('echo "Hello World";');

// Retrieve an array of tokens (same as php function token_get_all)
const tokens = parser.tokenGetAll('<?php echo "Hello World";');

// Load a static file (Note: this file should exist on your computer)
const phpFile = fs.readFileSync("./example.php");

// Log out results
console.log("Eval parse:", eval);
console.log("Tokens parse:", tokens);
console.log("File parse:", parser.parseCode(phpFile));

Sample AST output

{
  'kind': 'program',
  'children': [
    {
      'kind': 'echo',
      'arguments': [
        {
          'kind': 'string',
          'isDoubleQuote': true,
          'value': 'Hello World'
        }
      ]
    }
  ]
}

API Overview

The main API exposes a class with the following methods :

  • parseEval(String|Buffer) : parse a PHP code in eval style mode (without php open tags)
  • parseCode(String|Buffer, String filename) : parse a PHP code by using php open tags.
  • tokenGetAll(String|Buffer) : retrieves a list of all tokens from the specified input.

You can also pass options that change the behavior of the parser/lexer.

Documentation

You can add here your own project by opening an issue request.

License

This library is released under BSD-3 license clause.

Keywords

FAQs

Last updated on 07 Jun 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc