52

如何通过分隔符拆分字符串,但如果它被转义则不行?例如,我有一个字符串:

1|2\|2|3\\|4\\\|4

定界符 is|和转义定界符 is \|。此外,我想忽略转义\\|的反斜杠,所以 in|仍然是一个分隔符。

所以使用上面的字符串,结果应该是:

[0] => 1
[1] => 2\|2
[2] => 3\\
[3] => 4\\\|4
4

5 回答 5

107

使用黑魔法:

$array = preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $string);

\\\\.匹配后跟一个字符的反斜杠,(*SKIP)(*FAIL)跳过它并\|匹配您的分隔符。

于 2011-06-05T15:20:46.003 回答
11

而不是split(...),使用某种像词法标记器一样操作的“扫描”功能更直观。在 PHP 中,这将是preg_match_all函数。你只是说你想匹配:

  1. \a or以外的东西|
  2. 或 a\后跟 a\|
  3. 至少重复 #1 或 #2 一次

以下演示:

$input = "1|2\\|2|3\\\\|4\\\\\\|4";
echo $input . "\n\n";
preg_match_all('/(?:\\\\.|[^\\\\|])+/', $input, $parts);
print_r($parts[0]);

将打印:

1|2\|2|3\\|4\\\|4

Array
(
    [0] => 1
    [1] => 2\|2
    [2] => 3\\
    [3] => 4\\\|4
)
于 2011-10-02T18:06:53.533 回答
4

最近我设计了一个解决方案:

$array = preg_split('~ ((?<!\\\\)|(?<=[^\\\\](\\\\\\\\)+)) \| ~x', $string);

但黑魔法解法还是快了三倍。

于 2011-06-06T05:52:08.753 回答
4

对于未来的读者,这是一个通用的解决方案。它基于 NikiC 的想法(*SKIP)(*FAIL)

function split_escaped($delimiter, $escaper, $text)
{
    $d = preg_quote($delimiter, "~");
    $e = preg_quote($escaper, "~");
    $tokens = preg_split(
        '~' . $e . '(' . $e . '|' . $d . ')(*SKIP)(*FAIL)|' . $d . '~',
        $text
    );
    $escaperReplacement = str_replace(['\\', '$'], ['\\\\', '\\$'], $escaper);
    $delimiterReplacement = str_replace(['\\', '$'], ['\\\\', '\\$'], $delimiter);
    return preg_replace(
        ['~' . $e . $e . '~', '~' . $e . $d . '~'],
        [$escaperReplacement, $delimiterReplacement],
        $tokens
    );
}

试一试:

// the base situation:
$text = "asdf\\,fds\\,ddf,\\\\,f\\,,dd";
$delimiter = ",";
$escaper = "\\";
print_r(split_escaped($delimiter, $escaper, $text));

// other signs:
$text = "dk!%fj%slak!%df!!jlskj%%dfl%isr%!%%jlf";
$delimiter = "%";
$escaper = "!";
print_r(split_escaped($delimiter, $escaper, $text));

// delimiter with multiple characters:
$text = "aksd()jflaksd())jflkas(('()j()fkl'()()as()d('')jf";
$delimiter = "()";
$escaper = "'";
print_r(split_escaped($delimiter, $escaper, $text));

// escaper is same as delimiter:
$text = "asfl''asjf'lkas'''jfkl''d'jsl";
$delimiter = "'";
$escaper = "'";
print_r(split_escaped($delimiter, $escaper, $text));

输出:

Array
(
    [0] => asdf,fds,ddf
    [1] => \
    [2] => f,
    [3] => dd
)
Array
(
    [0] => dk%fj
    [1] => slak%df!jlskj
    [2] => 
    [3] => dfl
    [4] => isr
    [5] => %
    [6] => jlf
    )
Array
(
    [0] => aksd
    [1] => jflaksd
    [2] => )jfl'kas((()j
    [3] => fkl()
    [4] => as
    [5] => d(')jf
)
Array
(
    [0] => asfl'asjf
    [1] => lkas'
    [2] => jfkl'd
    [3] => jsl
)

注意:存在理论水平问题:implode('::', ['a:', ':b'])implode('::', ['a', '', 'b'])结果相同的字符串:'a::::b'。内爆也是一个有趣的问题。

于 2014-11-25T19:59:48.803 回答
-1

正则表达式非常缓慢。更好的方法是在拆分之前从字符串中删除转义字符,然后将它们放回:

$foo = 'a,b|,c,d||,e';

function splitEscaped($str, $delimiter,$escapeChar = '\\') {
    //Just some temporary strings to use as markers that will not appear in the original string
    $double = "\0\0\0_doub";
    $escaped = "\0\0\0_esc";
    $str = str_replace($escapeChar . $escapeChar, $double, $str);
    $str = str_replace($escapeChar . $delimiter, $escaped, $str);

    $split = explode($delimiter, $str);
    foreach ($split as &$val) $val = str_replace([$double, $escaped], [$escapeChar, $delimiter], $val);
    return $split;
}

print_r(splitEscaped($foo, ',', '|'));

在“,”上拆分,但如果用“|”转义则不会。它还支持双重转义,所以“||” 变成一个“|” 分裂发生后:

Array ( [0] => a [1] => b,c [2] => d| [3] => e ) 
于 2015-01-02T19:27:16.540 回答