Regex Split String
I'm using the first time regex and I want to split a stri开发者_JS百科ng in three vars
Target: '[|'.$1.'||'.$2.'||'.$3.'|]'; //each single var.
what I have:
preg_match_all("[|(.*)||(.*)||(.*)|]", $loadedList, $result);
I'm really getting crazy, therfore your help is more than wellcome ;-) regards Simon
What about this? It will work for a variable amout of items.
$result = explode('||', preg_replace('/(^\[\||\|\]$)/', '', $loadedList));
You need to escape the special characters:
preg_match_all("/\[\|(.*)\|\|(.*)\|\|(.*)\|\]/", $loadedList, $result);
|
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 ]
.
精彩评论