2

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?

1

5 Answers 5

2

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
)
Sign up to request clarification or add additional context in comments.

1 Comment

Pipe is also treated as a literal pipe inside character class not OR.
2

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);

Comments

2

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

Comments

1

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);

2 Comments

gotcha. extra backslash for the win.
I posted a link in comments after the answer. Seems in some or all cases 4 \\\\ is the most correct.
1

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

(\w+)

Regular expression visualization

Debuggex Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.