user921509
user921509

Reputation: 88

split string in php using regex

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 moment, I'm using:

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

But it returns myvalue^nao

Any ideas?

Upvotes: 0

Views: 536

Answers (2)

mu is too short
mu is too short

Reputation: 434635

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

Upvotes: 2

poplitea
poplitea

Reputation: 3737

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

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

Upvotes: 0

Related Questions