سؤال

I need this string: "test1 test2-test3\test4" to be split into 4 strings:

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

What am I doing wrong?

I've tried this:

<?php

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

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

And this:

<?php

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

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

To no avail. For some reason it doesn't split by backslash- why?

هل كانت مفيدة؟

المحلول 4

Try three slashes \:

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

And you don't need the alternation in the character class:

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

نصائح أخرى

You don't need to place pipe inside character class.

You can use:

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

OUTPUT:

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

Your first mistake is not an error, but it looks odd. You do not need to place pipe(|) inside the character class []. Simply place the character inside []

If you are dealing with backslash, then you have to use several slashes, one for the slash itself, and another two for the escape character.

Here it is based on above lines:

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

Use this ..

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

OUTPUT :

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

enter image description here

Try (\w+)? It seems to do exactly what you need in debuggex. Click through to see demo.

(\w+)

Regular expression visualization

Debuggex Demo

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top