Reputation: 2478
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);
Upvotes: 1
Views: 572
Reputation: 19547
You need to escape the special characters:
preg_match_all("/\[\|(.*)\|\|(.*)\|\|(.*)\|\]/", $loadedList, $result);
Upvotes: 3
Reputation: 31006
What about this? It will work for a variable amout of items.
$result = explode('||', preg_replace('/(^\[\||\|\]$)/', '', $loadedList));
Upvotes: 4
Reputation: 336468
|
is a metacharacter in regular expressions (meaning "or"), so it needs to be escaped if meant to match literally. Furthermore, [...]
is regex syntax for a character class, meaning "any one of the characters between [...]
). And finally, you need delimiters around your regular expression.
You could try
preg_match_all("/[^\[\]\|]+/")
to match all non-|
/[
/]
strings, i. e. everything except |
, [
or ]
.
Upvotes: 1