Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php pcre正则表达式-通过反斜杠拆分文本_Php_Regex_Pcre - Fatal编程技术网

Php pcre正则表达式-通过反斜杠拆分文本

Php pcre正则表达式-通过反斜杠拆分文本,php,regex,pcre,Php,Regex,Pcre,我需要将以下字符串:“test1 test2-test3\test4”拆分为4个字符串: Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 ) 我做错了什么 我试过这个: <?php $aa = 'test1 test2-test3\test4'; $arr = preg_split('#[\s|\|-]+#u', $aa); print

我需要将以下字符串:“test1 test2-test3\test4”拆分为4个字符串:

Array
(
    [0] => test1
    [1] => test2
    [2] => test3
    [3] => test4
)
我做错了什么

我试过这个:

<?php

    $aa = 'test1 test2-test3\test4';
    $arr = preg_split('#[\s|\|-]+#u', $aa);
    print_r($arr);

?>
Array
(
    [0] => test1
    [1] => test2
    [2] => test3\test4
)

排列
(
[0]=>test1
[1] =>测试2
[2] =>test3\test4
)
这是:

<?php

    $aa = 'test1 test2-test3\test4';
    $arr = preg_split('#[\s|\\|-]+#u', $aa);
    print_r($arr);

?>
Array
(
    [0] => test1
    [1] => test2
    [2] => test3\test4
)

排列
(
[0]=>test1
[1] =>测试2
[2] =>test3\test4
)

无济于事。出于某种原因,它不会被反斜杠分开-为什么

尝试三个斜杠
\

    $arr = preg_split('#[\s|\\\|-]+#u', $aa);
您不需要在character类中进行替换:

    $arr = preg_split('#[\s\\\-]+#u', $aa);
是否尝试
(\w+)
?它似乎正是您在debuggex中所需要的。点击查看演示

(\w+)

使用这个

  $arr = preg_split('#[\s|\\\|-]+#u', $aa);         
                           ^^ //<-------------- Add two more backslashes      

您不需要在角色类中放置管道

您可以使用:

$aa = 'test1 test2-test3\test4';
$arr = preg_split('#[-\s\\\\]+#u', $aa);
print_r($arr);
输出:
你的第一个错误不是错误,但看起来很奇怪。无需将管道(
|
)放置在字符类
[]
内。只需将字符放入
[]

如果要处理反斜杠,则必须使用多个斜杠,一个用于斜杠本身,另两个用于转义字符

这里是基于以上几行:

$arr = preg_split('#[\s\\\-]+#u', $aa);

明白了。赢了额外的反斜杠。我在回答后的评论中贴了一个链接。似乎在某些或所有情况下4\\\是最正确的。哇。不知道:)+1管道在字符类中也被视为文字管道,而不是。
Array
(
    [0] => test1
    [1] => test2
    [2] => test3
    [3] => test4
)
$arr = preg_split('#[\s\\\-]+#u', $aa);