S
S
Sergey Pugovkin2016-01-11 15:03:03
PHP
Sergey Pugovkin, 2016-01-11 15:03:03

How to split a string in which the delimiter (separator) can be escaped, and this must be taken into account?

There is a line like:

данные 1 | данные 2 | данные 3.1 \| данные 3.2 \| данные 3.3 | данные 4

It must be split by the symbol |
At the same time, the fate is that it is escaped in some places \|
Those. you need to get an array like:
[
    'данные 1',
    'данные 2',
    'данные 3.1 \| данные 3.2 \| данные 3.3',
    'данные 4',
];

The usual explode here, unfortunately, is powerless.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Io, 2016-01-11
@vawsan

Do a pre- replace sequence \| with a tricky combination like {-del-} , and beat the result with the | character. And in the resulting array, replace {-del-} back with \.

B
Bloby, 2016-01-11
@Bloby

the easiest way to do this is:

$str = stripslashes('данные 1 | данные 2 | данные 3.1 \| данные 3.2 \| данные 3.3 | данные 4');
$res = explode(' | ', $str);
var_dump($res);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question