php - Extracting specific parts of a predictably structured string with unpredictable contents -


ok, have complex problem guys.

i trying extract values load of old data. it's bunch of strings 7 parts concatenated ||

test1||keep||1:1||test||3462||7885||test 

rules

  • each section of string have character in it, except | or 2 arrows <> (see further down) reserved separators.

  • any of sections empty.

e.g. in 1 first 1st, 5th , 6th sections empty, , 3rd contains lots of non-alphanumeric characters.

||keep||test's\ (o-kay?).go_od||test||||||test 

furthermore...

some of strings made of multiple ones of these 7 pieces, further separated <>

test1||keep||1:1||test||3462||7885||test<>test1||keep||1:1||test||3462||7885||test<>test1||keep||1:1||test||3462||7885||test 

remember, of inner sections empty.

test54||keep||test's\ (o-kay?).go_od||test||||||<>test||keep||test545's'/.||test||||test||test 

the goal

extract second part of every string, , put array. in examples above, every part has word keep inside.

so example:

||keep||test's\ (o-kay?).go_od||test||||||test 

i want get:

array('keep') 

and example:

test1||keep-me||1:1||test||3462||7885||test<>||keep||||||3462||7885||<>test1||keep-me-too!||1:1||test||3462||||test 

it can seen 3 different strings separated <>:

test1||keep-me||1:1||test||3462||7885||test  ||keep||||||3462||7885||  test1||keep-me-too!||1:1||test||3462||||test 

and want extract:

array('keep-me', 'keep', 'keep-me-too!') 

notes

i have tried doing preg_match look-behind doesn't searching non-fixed length strings.

i cannot change data. old data have work with.

$array = []; $strings = explode('<>', $yourcontent); foreach ($strings $string) {     $array[] = explode('||', $string)[1]; } 

this uses array dereferencing introduced in php 5.4.


Comments