Fahid Mohammad
Fahid Mohammad

Reputation: 930

Split in series PHP

i have this string ++++++1DESIGNRESULTSM25Fe415(Main)

and i have similar string about 2000 lines from which i want to split these..

[++++++] [1] [DESIGNRESULTS] [M25] [Fe415] [(Main)]

from the pattern only the 2nd 4h and 5th value changes

eg.. ++++++2DESIGNRESULTSM30Fe418(Main) etc..

what i actually want is:

  1. Split the first value [++++++]
  2. Split the value after 4 Character of [DESIGNRESULTS] so ill get this [M25]
  3. Split the value before 4 Character of [(Main)] so ill get this [Fe415]
  4. After all this done store the final chunk of piece in an array.

the similar output what i want is

Array ( [0] => 1  [1] => M25  [2] => Fe415 ) 

Please help me with this...

Thanks in advance :)

Upvotes: 0

Views: 76

Answers (2)

Jaspreet Chahal
Jaspreet Chahal

Reputation: 2750

How about this

 $str = "++++++1DESIGNRESULTSM25Fe415(Main)";

 $match = array();
 preg_match("/^\+{0,}(\d)DESIGNRESULTS(\w{3})(\w{5})/",$str,$match);
 array_shift($match);
 print_r($match);

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191729

Your data split needs are a bit unclear. A regular expression that will get separate matches on each of the chunks you first specify:

(\++)(\d)(DESIGNRESULTS)(M\d\d)(Fe\d\d\d)(\(Main\))

If you only need the two you are asking for at the end, you can use

(\d)DESIGNRESULTS(M\d\d)(Fe\d\d\d)

You could also replace \d\d with \d+ if the number of digits is unknown.

However, based on your examples it looks like each string chunk is a consistent length. It would be even faster to use

array(
   substr($string, 6, 1)
   //...
)

Upvotes: 1

Related Questions