PHPackages                             mojahed/mparser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. mojahed/mparser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

mojahed/mparser
===============

MParser is a powerful PHP package that simplifies parsing complex strings with custom tokens and expressions. With a flexible trait-based architecture, it enables effortless creation and extension of parsing functionality. Easily integrate MParser into your projects for efficient string processing and data extraction in a user-friendly manner.

2.0.0(10mo ago)0145MITPHPPHP &gt;=8.1

Since Nov 4Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/md-mojahed/mparser)[ Packagist](https://packagist.org/packages/mojahed/mparser)[ RSS](/packages/mojahed-mparser/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependenciesVersions (3)Used By (0)

MParser - Powerful String Parsing Library
=========================================

[](#mparser---powerful-string-parsing-library)

[![Latest Version](https://camo.githubusercontent.com/73310b53a05d5c9ccad2f8adb83095cd495e056c71b666a16048c4e0ebd42b42/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7461672f6d642d6d6f6a616865642f6d7061727365722e7376673f7374796c653d666c61742d737175617265)](https://github.com/md-mojahed/mparser/tags)[![Total Downloads](https://camo.githubusercontent.com/302e8ea9dc6936ce9cdae02f2fa9295334d6ad093972571938a121e98da70022/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f6a616865642f6d7061727365722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mojahed/mparser)[![GitHub stars](https://camo.githubusercontent.com/cf435da197e82379c6d583ca688f217502e9de117e3e1be8191b1b22e8deb5ea/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6d642d6d6f6a616865642f6d7061727365723f7374796c653d666c61742d737175617265)](https://github.com/md-mojahed/mparser/stargazers)[![GitHub views](https://camo.githubusercontent.com/69d1718b40c45a299d2388b730f0766bbfbc78625dbe2450177ecbbf794d4f06/68747470733a2f2f6b6f6d617265762e636f6d2f67687076632f3f757365726e616d653d6d6f6a61686564267265706f3d6d706172736572267374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/69d1718b40c45a299d2388b730f0766bbfbc78625dbe2450177ecbbf794d4f06/68747470733a2f2f6b6f6d617265762e636f6d2f67687076632f3f757365726e616d653d6d6f6a61686564267265706f3d6d706172736572267374796c653d666c61742d737175617265)

MParser is a PHP package that simplifies parsing complex strings with custom tokens and expressions. It allows you to effortlessly create and extend parsing functionality using a flexible trait-based architecture. Easily integrate MParser into your projects for efficient string processing and data extraction in a user-friendly manner.

Installation
------------

[](#installation)

You can install the MParser package using [Composer](https://getcomposer.org/). Run the following command:

```
composer require mojahed/mparser
```

Basic Usage
-----------

[](#basic-usage)

To use MParser, follow these steps:

1. Import the necessary class: `use Mojahed\MParser;`
2. Parse the string with the provided data:

    ```
    $data = [
        // Your data array here
    ];

    $string = "Your input string here";
    $result = MParser::parse($string, $data);
    ```

Available Methods
-----------------

[](#available-methods)

MParser provides the following methods for parsing and replacing tokens in the input string:

### `exp`

[](#exp)

Splits the token and returns the value at the specified index.

**Example:**

```
$data = ['numbers' => '1-2-3-4-5'];
$string = "The second number is: {{numbers}[exp(-, 2)]}.";

// Result: "The second number is: 2."
```

!Note : exp doesn't supports (,) comma as first parameter

### `cut`

[](#cut)

Extracts a substring from the token based on the start and length specified.

**Example:**

```
$data = ['word' => 'hello'];
$string = "The first three letters are: {{word}[cut(1, 3)]}.";

// Result: "The first three letters are: hel."
```

### `cut_back`

[](#cut_back)

Extracts a substring from the end of the token based on the length specified.

**Example:**

```
$data = ['word' => 'hello'];
$string = "The last three letters are: {{word}[cut_back(3)]}.";

// Result: "The last three letters are: llo."
```

### `reverse`

[](#reverse)

Reverses the characters of the token.

**Example:**

```
$data = ['word' => 'hello'];
$string = "The reversed word is: {{word}[reverse()]}.";

// Result: "The reversed word is: olleh."
```

### `sum`

[](#sum)

Adds the specified number to the token (assumed to be numeric).

**Example:**

```
$data = ['number' => 5];
$string = "The sum is: {{number}[sum(10)]}.";

// Result: "The sum is: 15."
```

### `sub`

[](#sub)

Subtracts the specified number from the token (assumed to be numeric).

**Example:**

```
$data = ['number' => 15];
$string = "The difference is: {{number}[sub(10)]}.";

// Result: "The difference is: 5."
```

### `mul`

[](#mul)

Multiplies the token with the specified number (assumed to be numeric).

**Example:**

```
$data = ['number' => 5];
$string = "The result is: {{number}[mul(3)]}.";

// Result: "The result is: 15."
```

### `div`

[](#div)

Divides the token by the specified number (assumed to be numeric). Returns 0 if the divisor is zero.

**Example:**

```
$data = ['number' => 15];
$string = "The result is: {{number}[div(3)]}.";

// Result: "The result is: 5."
```

### `power`

[](#power)

Raises the token to the power of the specified number (assumed to be numeric).

**Example:**

```
$data = ['number' => 2];
$string = "The result is: {{number}[power(3)]}.";

// Result: "The result is: 8."
```

### `upper`

[](#upper)

Converts the token to uppercase.

**Example:**

```
$data = ['word' => 'hello'];
$string = "The uppercase word is: {{word}[upper()]}.";

// Result: "The uppercase word is: HELLO."
```

### `lower`

[](#lower)

Converts the token to lowercase.

**Example:**

```
$data = ['word' => 'HELLO'];
$string = "The lowercase word is: {{word}[lower()]}.";

// Result: "The lowercase word is: hello."
```

### `capitalize`

[](#capitalize)

Capitalizes the first character of the token while converting the rest to lowercase.

**Example:**

```
$data = ['word' => 'hello'];
$string = "The capitalized word is: {{word}[capitalize()]}.";

// Result: "The capitalized word is: Hello."
```

### `replace`

[](#replace)

Replaces occurrences of the search string with the replacement string in the token.

**Example:**

```
$data = ['sentence' => 'The quick brown fox'];
$string = "The modified sentence is: {{sentence}[replace(quick, lazy)]}.";

// Result: "The modified sentence is: The lazy brown fox."
```

### `length`

[](#length)

Returns the length of the token (number of characters).

**Example:**

```
$data = ['word' => 'hello'];
$string = "The length of the word is: {{word}[length()]}.";

// Result: "The length of the word is: 5."
```

### `concat`

[](#concat)

Concatenates the token with the specified string.

**Example:**

```
$data = ['word' => 'hello'];
$string = "The concatenated word is: {{word}[concat( world)]}.";

// Result: "The concatenated word is: hello world."
```

`Multiple Expression`
=====================

[](#multiple-expression)

Use multiple expression at a time.

**Example:**

```
$data = ['word' => 'hello'];
$string = "The length of hello world is: {{word}[concat( world)][upper()][length()]}.";

// Result: "The length of hello world is: 11."
```

Custom Methods
--------------

[](#custom-methods)

You can add your own custom methods to extend the functionality of MParser. Simply create a new class that inherits from `MParser` and define your custom methods. For example:

```
class ChildParser extends MParser
{
    function __construct($string, array $dataset)
    {
        parent::__construct($string, $dataset);
    }

    public function _reverse($token, $args)
    {
        return strrev($token);
    }
}
```

By creating the `ChildParser` class and defining the `_reverse` method, you can now use `[reverse()]` in your input string to call the custom method.

Contributing
------------

[](#contributing)

If you find any issues, have suggestions, or want to add new features, feel free to create an issue or submit a pull request on GitHub.

License
-------

[](#license)

This package is open-source and available under the [MIT License](https://opensource.org/licenses/MIT). Feel free to use and modify it according to your needs.

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance55

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~670 days

Total

2

Last Release

302d ago

Major Versions

1.0.0 → 2.0.02025-09-04

PHP version history (2 changes)1.0.0PHP &gt;=7.2

2.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/92214734?v=4)[Md Mojahedul Islam](/maintainers/md-mojahed)[@md-mojahed](https://github.com/md-mojahed)

---

Top Contributors

[![md-mojahed](https://avatars.githubusercontent.com/u/92214734?v=4)](https://github.com/md-mojahed "md-mojahed (13 commits)")

### Embed Badge

![Health badge](/badges/mojahed-mparser/health.svg)

```
[![Health](https://phpackages.com/badges/mojahed-mparser/health.svg)](https://phpackages.com/packages/mojahed-mparser)
```

###  Alternatives

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M47](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

45153.1k6](/packages/jstewmc-rtf)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

113.2k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
