开发者

Regex Split String

开发者 https://www.devze.com 2023-01-28 22:28 出处:网络
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.

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 ].

0

精彩评论

暂无评论...
验证码 换一张
取 消