将反斜杠分隔的字符串转换为关联数组 [英] Convert backslash-delimited string into an associative array

查看:56
本文介绍了将反斜杠分隔的字符串转换为关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

I have a string like this:

key1\value1\key2\value2\key3\value3\key4\value4\key5\value5

我希望它是一个关联数组,以便我可以这样做:

And I'd like it to be an associative array so that I can do:

echo $myArray['key1']; // prints value1
echo $myArray['key3']; // prints value3
//etc...

我知道我可以在反斜杠上爆炸,但不知道如何从那里开始.

I know I can explode on the backslash, but not sure how to go from there.

推荐答案

通过 regex"http://php.net/preg_match_all" rel="noreferrer">preg_match_allarray_combine 通常是最短和最快的选项:

Using a simple regex via preg_match_all and array_combine is often the shortest and quickest option:

 preg_match_all("/([^\\\\]+)\\\\([^\\\\]+)/", $string, $p);
 $array = array_combine($p[1], $p[2]);

现在这当然是一个特例.keysvalues 都由 \ 反斜杠分隔,它们都是成对的.由于必要的双重转义,正则表达式也有点长.

Now this is of course a special case. Both keys and values are separated by a \ backslash, as are all pairs of them. The regex is also a bit lengthier due to the necessary double escaping.

然而,这个方案可以推广到其他 key:value, 样式的字符串.

However this scheme can be generalized to other key:value,-style strings.

常见的变体包括 := 作为键/值分隔符,以及 & 等作为对分隔符.在这种情况下,正则表达式变得相当明显(使用 /x 标志以提高可读性):

Common variations include : and = as key/value separators, and , or & and others as pair delimiters. The regex becomes rather obvious in such cases (with the /x flag for readability):

 #                    ↓    ↓    ↓
 preg_match_all("/ ([^:]+) : ([^,]+) /x", $string, $p);
 $array = array_combine($p[1], $p[2]);

这使得将 :, 交换为其他分隔符变得非常容易.

Which makes it super easy to exchange : and , for other delimiters.

  • 等号 = 而不是 : 冒号.
  • 例如 \\t 作为对分隔符(制表符分隔的键:值列表)
  • 经典的 &; 作为键=值对之间的分隔符.
  • 或者只是 \\s 个空格或 \\n 个换行符.
  • Equal signs = instead of : colons.
  • For example \\t as pair delimiter (tab-separated key:value lists)
  • Classic & or ; as separator between key=value pairs.
  • Or just \\s spaces or \\n newlines even.

您可以通过在键/值/对之间允许不同的分隔符来使其更加灵活/宽容:

You can make it more flexible/forgiving by allowing different delimiters between keys/values/pairs:

 #                    ↓      ↓       ↓
 preg_match_all("/ ([^:=]+) [:=]+ ([^,+&]+) /x", $string, $p);

其中 key=value,key2:value2++key3==value3 都可以工作.这对于更多人性化的用户(也就是非技术用户)来说更有意义.

Where both key=value,key2:value2++key3==value3 would work. Which can make sense for more human-friendlinies (AKA non-technical users).

通常,除了经典的 key 标识符之外,您可能想要禁止任何东西.只需使用 \w+ 字串模式使正则表达式跳过不需要的出现:

Oftentimes you may want to prohibit anything but classic key identifiers. Just use a \w+ word string pattern to make the regex skip over unwanted occurences:

 #                   ↓   ↓    ↓
 preg_match_all("/ (\w+) = ([^,]+) /x", $string, $p);

这是最简单的白名单方法.如果 OTOH 你想事先assert/约束整个键/值字符串,然后制作一个单独的 preg_match("/^(\w+=[^,]+(,|$))+/", ...

This is the most trivial whitelisting approach. If OTOH you want to assert/constrain the whole key/value string beforehand, then craft a separate preg_match("/^(\w+=[^,]+(,|$))+/", …

您可以跳过一些后处理步骤(例如 trim 键和值)加上少量:

You can skip a few post-processing steps (such as trim on keys and values) with a small addition:

 preg_match_all("/ \s*([^=]+) \s*=\s* ([^,]+) (?<!\s) /x", $string, $p);

或者例如可选的引号:

 preg_match_all("/ \s*([^=]+) \s*=\s* '? ([^,]+) (?<![\s']) /x", $string, $p);

INI 风格的提取

你可以制作一个基线 INI 文件提取方法:

INI-style extraction

And you can craft a baseline INI-file extraction method:

 preg_match_all("/^ \s*(\w+) \s*=\s* ['\"]?(.+?)['\"]? \s* $/xm", $string, $p);

请注意,这只是常见 INI 方案的粗略子集.

Please note that this is just a crude subset of common INI schemes.

如果你已经有一个 key=value&key2=value2 字符串,那么 parse_str 就像一个魅力.但是通过将它与 strtr 结合起来,甚至可以处理不同的其他分隔符:

If you have a key=value&key2=value2 string already, then parse_str works like a charm. But by combining it with strtr can even process varying other delimiters:

 #                         ↓↓    ↑↑
 parse_str(strtr($string, ":,", "=&"), $pairs);

它有几个优点和缺点:

  • 甚至比两行正则表达式方法还要短.
  • 预定义了一个众所周知的转义机制,例如用于特殊字符的%2F).
  • 不允许使用不同的分隔符或其中的未转义分隔符.
  • 自动将 keys[]= 转换为数组,您可能需要也可能不需要.
  • Even shorter than the two-line regex approach.
  • Predefines a well-known escaping mechanism, such as %2F for special characters).
  • Does not permit varying delimiters, or unescaped delimiters within.
  • Automatically converts keys[]= to arrays, which you may or may not want though.

您会找到许多手动键/值字符串扩展的示例.虽然这通常是更多的代码.由于优化假设,explode 在 PHP 中被过度使用.然而,由于手动foreach 和数组集合,分析后通常会变慢.

You'll find many examples of manual key/value string expansion. Though this is often more code. explode is somewhat overused in PHP due to optimization assumptions. After profiling often turns out to be slower however due to the manual foreach and array collection.

这篇关于将反斜杠分隔的字符串转换为关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆