开发者

split string in php using regex

开发者 https://www.devze.com 2023-04-01 13:36 出处:网络
how can I split this string: ^^^*|^^^*|^^^*|^^^*|^^^*|myvalue^nao^nao^nao*|myvalue^nao^nao^nao*|myvalue^nao^nao^nao*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*

how can I split this string:

|^^^*|^^^*|^^^*|^^^*|^^^*|myvalue^nao^nao^nao*|myvalue^nao^nao^nao*|myvalue^nao^nao^nao*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*|^^^*

so I can only get the value "mayvalue".

For the mo开发者_开发知识库ment, I'm using:

$text = preg_split("/[^\|(*.?)\^$]/", $other);

But it returns myvalue^nao

Any ideas?


You could split twice, once on | and again on ^; if your big string is in $input, then:

$pipes = preg_split('/\|/', $input);
$want  = preg_split('/\^/', $pipes[6]);

Then $want[0] has what you're after. This would probably be easier than trying to come up with one regex to split with.

Demo: http://ideone.com/pxvay

Since shesek hasn't come back I'll include their suggested approach. You can also use explode twice since you're working with simple delimiters:

$pipes = explode('|', $input);
$want  = explode('^', $pipes[6]);

and again $want[0] has what you're looking for.

And a demo of this approach: http://ideone.com/SwGEH


This will get all "myvalue" and other strings placed after | and before ^ in an array, $matches[1]:

$text = preg_match_all("/\|(.*?)\^.*?\^.*?\^.*?\*/", $other, $matches);
0

精彩评论

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

关注公众号