如何根据指示顺序的输入掩码将字符串解析为多个变量?
How to parse a string into multiple variables based on an input mask indicating the order?
假设你从这个开始:
$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);
将 $result
转换为以下已定义和填充的变量的最佳方法是什么?
(另请记住,$mask
的顺序可能会改变。)
$name = '?';
$user_count = '?';
$acyr = '?';
$audience = '?';
我曾尝试使用 preg_split()
/ list()
,但我希望 $mask
来管理变量的顺序,而不必将其转换为复杂的正则表达式。
基本上我需要一个简单的方法 parsing/splitting 基于包含占位符的掩码将字符串转换为多个变量。
可以通过更改占位符的顺序或添加新的标点符号来更改掩码,但逻辑足够灵活以生成变量。
<?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
假设你从这个开始:
$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);
将 $result
转换为以下已定义和填充的变量的最佳方法是什么?
(另请记住,$mask
的顺序可能会改变。)
$name = '?';
$user_count = '?';
$acyr = '?';
$audience = '?';
我曾尝试使用 preg_split()
/ list()
,但我希望 $mask
来管理变量的顺序,而不必将其转换为复杂的正则表达式。
基本上我需要一个简单的方法 parsing/splitting 基于包含占位符的掩码将字符串转换为多个变量。
可以通过更改占位符的顺序或添加新的标点符号来更改掩码,但逻辑足够灵活以生成变量。
<?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