Search code examples
phpregextext-extractiontext-parsingdelimited

Extract values from a double-pipe- separated string also wrapped in pipes and square braces


I want to parse a string into three variables. My formatted input string starts with [|, the 3 sought values are delimited by ||, and the string ends with |].

Intention:

'[|'.$1.'||'.$2.'||'.$3.'|]'

What I have tried doesn't produce the desired results:

preg_match_all("[|(.*)||(.*)||(.*)|]", $loadedList, $result);

Solution

  • What about this? It will work for a variable amout of items.

    $result = explode('||', preg_replace('/(^\[\||\|\]$)/', '', $loadedList));