2

Assuming you start with this:

$mask = "%name% (%user_count%) - %acyr% - %audience%";
$data = [
    '%name%'       => $group['name'],
    '%user_count%' => $group['user_count'],
    '%acyr%'       => $group['acyr'],
    '%audience%'   => $group['audience'],
];
$result = strtr($mask, $data);

What is the best way of reversing this such that you transform $result into the following defined and populated variables? (Also bear in mind that the order of $mask could change.)

$name       = '?';
$user_count = '?';
$acyr       = '?';
$audience   = '?';

I have tried using preg_split() / list() but I want $mask to govern the order of the variables without having to convert it into a complicated regex. Basically I need a simple method of parsing/splitting a string into multiple variables based on a mask containing placeholders.

u01jmg3
  • 712
  • 1
  • 11
  • 31
  • Where do you get the data in `$group`? Why not just get it from there again? – nerdlyist Mar 14 '17 at 12:28
  • From an API call which is expensive to repeat. I already have the data locally so I just need to parse and create the variables as described. – u01jmg3 Mar 14 '17 at 12:40
  • If it is that expensive why not store it in a `$_SESSION`? – nerdlyist Mar 14 '17 at 12:44
  • There are many groups and it isn't sensible to store all group data in the session. This is also going off topic from the question that I have asked. – u01jmg3 Mar 14 '17 at 12:56
  • 1
    It is suggesting other options which is perfectly on topic for comments. I was actually working on a solution as well it is not a simple one. – nerdlyist Mar 14 '17 at 13:01
  • Suffice to say I have explored other possibilities and I am currently seeing how well this one pads out. I do realise it is not simple to answer hence the need to ask for help. – u01jmg3 Mar 14 '17 at 13:04

1 Answers1

0

The mask can be changed by altering the order of the placeholders or adding new punctuation yet the logic is flexible enough to generate the variables.

PHP code demo

<?php
    $mask = "%name% (%user_count%) - %acyr% - %audience%";
    $data = [
        '%name%'       => 'Year 1',
        '%user_count%' => 999,
        '%acyr%'       => 2017,
        '%audience%'   => 'Staff and Students',
    ];
    $result = strtr($mask, $data);

    // Extract unique punctuation used in mask and form pattern for split
    $pattern = '/[' . preg_replace('/[\w\s%]/', '', count_chars($mask, 3)) . ']/';

    // Split, trim, remove blanks and reset array keys
    $variables = array_values(array_filter(array_map('trim', preg_split($pattern, $mask, -1, PREG_SPLIT_NO_EMPTY)), 'strlen'));
    $data      = array_values(array_filter(array_map('trim', preg_split($pattern, $result, -1, PREG_SPLIT_NO_EMPTY)), 'strlen'));

    foreach ($variables as $key => $variable_name) {
        $variable_name = str_replace('%', '', $variable_name);
        // Dynamically create variable
        ${$variable_name} = $data[$key];
    }

    var_dump("\$name: $name");             // Year 1
    var_dump("\$user_count: $user_count"); // 999
    var_dump("\$acyr: $acyr");             // 2017
    var_dump("\$audience: $audience");     // Staff and Students
u01jmg3
  • 712
  • 1
  • 11
  • 31