1

I have a simple PHP function for testing:

<?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;
}

?>

All data is general.


I have this simple text-block:

[/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.

I call this text block as a PHP varible: $MyTextBlock.


Now, I want to create a new PHP function: NewTestFunction, which parse the $MyTextBlock. The output text, which is equivalent with running MyOwnFunction('u', 'md5', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'); and MyOwnFunction('l', 'sha1', 'Fusce dignissim tincidunt dui id imperdiet.');. And, bellow is a returned text, which I want to have:

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.

How to parse this text-block as parameters of a function, in PHP? Is there any suggestion for me?


This question is not duplicated with any available question: my question is about the regex; and, an other question is about the equals.

Community
  • 1
  • 1
tran_hoan
  • 11
  • 3

1 Answers1

0

A way to do something like this is to use preg_replace_callback, example:

// 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;
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125