How to make this weird string explode in PHP?

I have a line like the following

DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string] 

The above line is a formatted view in groups that looks like this:

 AB[C]-DE-[F]-G-[H] 

I think I like to handle some of these groups, and I like to do something like an explosion.

I say because I tried this code:

 $string = 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]'; $parts = explode( '-', $string ); print_r( $parts ); 

and I get the following result:

 Array ( [0] => DAS [1] => 1111[DR [2] => Helpfull [3] => R] [4] => RUN [5] => [6] => [121668688374] [7] => N [8] => [+helpfull_+string] ) 

that this is not what i need.

I need the following output:

 Array ( [0] => DAS [1] => 1111[DR-Helpfull-R] [2] => RUN [3] => [4] => [121668688374] [5] => N [6] => [+helpfull_+string] ) 

Can someone suggest a nice and elegant way to blow this line the way I need it?

I forgot to mention that a string can have more or less groups. Examples:

 DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string] DAS-1111[DR-Helpfull-R]-RUN--[121668688374] DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]-anotherPart 

Update 1

As @axiac mentioned, preg_split can do the job. But can you please help with regex?

I tried this, but it seems that this is not true:

(?!\]\-)\-

+5
source share
3 answers

Code:

 $str = 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]'; $re = '/([^-[]*(?:\[[^\]]*\])?[^-]*)-?/'; $matches = array(); preg_match_all($re, $str, $matches); print_r($matches[1]); 

His conclusion:

 Array ( [0] => DAS [1] => 1111[DR-Helpfull-R] [2] => RUN [3] => [4] => [121668688374] [5] => N [6] => [+helpfull_+string] [7] => ) 

The output has an additional empty value at position 7 . It appears due to zero or one quantization repeater ( ? ) Placed at the end of the regex . A quantifier is required, because without it the last element (with index 6 ) cannot be matched.

Can you delete ? after the last - and ask about it, as always, with a dash ( - ). In this case, you must add an additional line to your input line.

Regular expression

 ( # start of the 1st subpattern # the captured value is returned in $matches[1] [^-[]* # match any character but '-' and '[', zero or more times (?: # start of a non-capturing subpattern \[ # match an opening square bracket ('[') [^\]]* # match any character but ']', zero or more times \] # match a closing square bracket (']') )? # end of the subpattern; it is optional (can appear 0 or 1 times) [^-]* # match any character but '-', zero or more times ) # end of the 1st subpattern -? # match an optional dash ('-') 
+5
source

Instead of exploding, you should try to match the following pattern:

 (?:^|-)([^-\[]*(?:\[[^\]]+\])?) 

Here is an example :

 $regex = '/(?:^|-)([^-\[]*(?:\[[^\]]+\])?)/'; $tests = array( 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]', 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]', 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]-anotherPart' ); foreach ($tests as $test) { preg_match_all($regex, $test, $result); print_r($result[1]); } 

Output:

 // DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string] Array ( [0] => DAS [1] => 1111[DR-Helpfull-R] [2] => RUN [3] => [4] => [121668688374] [5] => N [6] => [+helpfull_+string] ) // DAS-1111[DR-Helpfull-R]-RUN--[121668688374] Array ( [0] => DAS [1] => 1111[DR-Helpfull-R] [2] => RUN [3] => [4] => [121668688374] ) // DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]-anotherPart Array ( [0] => DAS [1] => 1111[DR-Helpfull-R] [2] => RUN [3] => [4] => [121668688374] [5] => N [6] => [+helpfull_+string] [7] => anotherPart ) 
+2
source

This case is ideal for the (*SKIP)(*FAIL) method. You want to split the string into hyphens if they are not inside the square brackets.

Easy. Just disqualify these hyphens as separators, for example:

Pattern: ~\[[^]]+\](*SKIP)(*FAIL)|-~ ( Sample demo )

Code: ( Demo )

 $strings=['DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]', 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]', 'DAS-1111[DR-Helpfull-R]-RUN--[121668688374]-N-[+helpfull_+string]-anotherPart']; foreach($strings as $string){ var_export(preg_split('~\[[^]]+\](*SKIP)(*FAIL)|-~',$string)); echo "\n\n"; } 

Output:

 array ( 0 => 'DAS', 1 => '1111[DR-Helpfull-R]', 2 => 'RUN', 3 => '', 4 => '[121668688374]', 5 => 'N', 6 => '[+helpfull_+string]', ) array ( 0 => 'DAS', 1 => '1111[DR-Helpfull-R]', 2 => 'RUN', 3 => '', 4 => '[121668688374]', ) array ( 0 => 'DAS', 1 => '1111[DR-Helpfull-R]', 2 => 'RUN', 3 => '', 4 => '[121668688374]', 5 => 'N', 6 => '[+helpfull_+string]', 7 => 'anotherPart', ) 
+1
source

Source: https://habr.com/ru/post/1247208/


All Articles