mojahed/mparser

MParser 是一个强大的 PHP 扩展包,它可以简化使用自定义标记和表达式解析复杂字符串。通过灵活的基于特质的架构,它可以轻松创建和扩展解析功能。轻松将 MParser 集成到项目中,以高效的方式处理字符串并进行数据提取。

1.0.0 2023-11-04 09:44 UTC

This package is auto-updated.

Last update: 2024-09-04 11:27:26 UTC


README

MParser 是一个 PHP 扩展包,它可以简化使用自定义标记和表达式解析复杂字符串。它允许您使用灵活的基于特质的架构轻松创建和扩展解析功能。轻松将 MParser 集成到项目中,以用户友好的方式高效处理字符串并进行数据提取。

安装

您可以使用 Composer 安装 MParser 扩展包。运行以下命令

composer require mojahed/mparser

基本用法

要使用 MParser,请按照以下步骤操作

  1. 导入必要的类: use Mojahed\MParser;

  2. 使用提供的数据解析字符串

    $data = [
        // Your data array here
    ];
    
    $string = "Your input string here";
    $result = MParser::parse($string, $data);

可用方法

MParser 提供以下方法来解析和替换输入字符串中的标记

exp

分割标记并返回指定索引处的值。

示例

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

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

!注意:exp 不支持将逗号 (,) 作为第一个参数

cut

根据指定的起始位置和长度从标记中提取子字符串。

示例

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

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

cut_back

根据指定的长度从标记末尾提取子字符串。

示例

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

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

reverse

反转标记中的字符。

示例

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

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

sum

将指定的数字加到标记上(假定是数字)。

示例

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

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

sub

从标记中减去指定的数字(假定是数字)。

示例

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

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

mul

将标记乘以指定的数字(假定是数字)。

示例

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

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

div

将标记除以指定的数字(假定是数字)。如果除数为零,则返回 0。

示例

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

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

power

将标记提升到指定数字的幂(假定是数字)。

示例

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

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

upper

将标记转换为大写。

示例

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

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

lower

将标记转换为小写。

示例

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

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

capitalize

将标记的第一个字符大写,其余字符小写。

示例

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

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

replace

在标记中替换搜索字符串与替换字符串的出现。

示例

$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

返回标记的长度(字符数)。

示例

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

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

concat

将标记与指定的字符串连接。

示例

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

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

多重表达式

一次使用多个表达式。

示例

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

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

自定义方法

您可以为 MParser 扩展功能添加自己的自定义方法。只需创建一个新的类,该类从 MParser 继承,并定义您的自定义方法。例如

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

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

通过创建 ChildParser 类并定义 _reverse 方法,您现在可以在输入字符串中使用 [reverse()] 来调用自定义方法。

贡献

如果您发现任何问题、有建议或想添加新功能,请随时在 GitHub 上创建问题或提交拉取请求。

许可证

此包是开源的,可在 MIT 许可证 下使用。请随意根据您的需求使用和修改它。