M
M
madc0de2018-04-12 12:25:47
Regular Expressions
madc0de, 2018-04-12 12:25:47

Extract only text from string via regexp regexp?

There are dynamic lines:
20 black/grey 20 black/
grey
20 black/grey/white 20 black/grey
/white
That is, numbers and so on can be added first. It is necessary to receive only what is separated by the sign "/"
In order to then split it into an array through explode.
Please help me with the regular expression . Otherwise
, my knowledge ended at the level [.*^ ].*\/.* and she is looking for the current 1 example

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stimulate, 2018-04-12
@madc0de

sandbox.onlinephpfunctions.com/code/f406a217317a5a...

N
Nick Sdk, 2018-04-12
@lidacriss

$text = '20 черный/серый
20черный/серый
20 черный/серый/белый
20черный/серый/белый';

$strings = preg_split('/\n/s', $text, null, PREG_SPLIT_NO_EMPTY);
$strings = array_map(function ($val){
    return explode('/', trim(preg_replace('/^\d+/', '', $val)));
}, $strings);

print_r($strings);

Array
(
    [0] => Array
        (
            [0] => черный
            [1] => серый
        )
    [1] => Array
        (
            [0] => черный
            [1] => серый
        )
    [2] => Array
        (
            [0] => черный
            [1] => серый
            [2] => белый
        )
    [3] => Array
        (
            [0] => черный
            [1] => серый
            [2] => белый
        )
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question