如何在PHP中将一个简单的文本块解析为函数的参数


How to parse a simple text-block as parameters of a function, in PHP?

我有一个简单的PHP函数用于测试:

<?php
function MyOwnFunction($CaseType, $HashType, $OrgStr) {
    if ($CaseType == 'u') {
        $NewStr = strtoupper($OrgStr);
    }
    if ($CaseType == 'l') {
        $NewStr = strtolower($OrgStr);
    }
    if ($HashType == 'md5') {
        $OutStr = md5($NewStr);
    }
    if ($HashType == 'sha1') {
        $OutStr = sha1($NewStr);
    }
    return $OutStr;
}
?>

所有数据都是通用的。


我有一个简单的文本块:

[/a_1]Lorem ipsum dolor sit amet, consectetur adipiscing elit.[a_1/]
Phasellus et commodo ligula.
[/b_2]Nulla finibus posuere nisl, ut ultrices dolor.[b_2/]
[/c_3]Fusce dignissim tincidunt dui id imperdiet.[c_3/]
Donec venenatis ipsum lacus, sit amet posuere enim gravida et.
---
[a_1] = u : md5.
[c_3] = l : sha1.

我将这个文本块称为PHP变量:$MyTextBlock


现在,我想创建一个新的PHP函数:NewTestFunction,它解析$MyTextBlock。输出文本,与运行MyOwnFunction('u', 'md5', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.');MyOwnFunction('l', 'sha1', 'Fusce dignissim tincidunt dui id imperdiet.');时的等效。下面是我想要的返回文本:

958bbb39b883fb80e852db91d15a80ca
Phasellus et commodo ligula.
[/b_2]Nulla finibus posuere nisl, ut ultrices dolor.[b_2/]
f98503d5c5cc6355895e049f5b0676d54588a6d6
Donec venenatis ipsum lacus, sit amet posuere enim gravida et.
---
[a_1] = u : md5.
[c_3] = l : sha1.

如何在PHP中将此文本块解析为函数的参数?有什么建议吗?


这个问题与任何可用的问题都不重复:我的问题是关于正则表达式;另一个问题是关于等于

这样做的一种方法是使用preg_replace_callback,例如:

// you need first to split the text at the last ---
$parts = preg_split('~'A(?>.*'K'R)*---'R's*~', $text);
// if the split succeeds:
if (isset($parts[1])) {
    list($text, $table) = $parts;
    $tablePtn = '~^'[(?<tag>[^]]*)] = (?<case>[ul]) : (?<hash>sha1|md5)'.$~m';
    if (preg_match_all($tablePtn, $table, $matches, PREG_SET_ORDER)) {
        $corr = [];
        foreach ($matches as $m)
            $corr[$m['tag']] = ['case' => $m['case'], 'hash' => $m['hash']];
        // build a pattern with known tags
        $textPtn = '~'[/(?<tag>' . implode('|', array_keys($corr)) . ')]'
                 . '(?<content> [^[]*+ (?:'[(?!'g<tag>/]|/'g<tag>])[^[]*)*+ )'
                 . ''['g{tag}/]~x';
        // the do...while($count) approach allows to deal with nested tags
        // Note that the pattern is build to match the innermost tag
        do {
            $text = preg_replace_callback($textPtn, function ($m) use ($corr) {
                $trans = $corr[$m['tag']];
                if ($trans['case'] == 'u')
                    $res = strtoupper($m['content']);
                elseif ($trans['case'] == 'l')
                    $res = strtolower($m['content']);
                if ($trans['hash'] == 'md5')
                    $res = md5($res);
                elseif ($trans['hash'] == 'sha1')
                    $res = sha1($res);
                return $res;
            }, $text, -1, $count);
        } while ($count);
    }
}
echo $text;