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

时间:2016-01-03 05:46:23

标签: php regex parsing text

我有一个简单的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.');。并且,bellow是一个返回的文本,我想要:

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中将此文本块解析为函数的参数?对我有什么建议吗?

  

这个问题与任何可用的问题不重复:我的问题是关于正则表达式;并且other question等于有关。

1 个答案:

答案 0 :(得分:0)

执行此类操作的方法是使用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;