0

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?

1
  • are you only trying to get the words myvalue? if so you can use a word boundry /bmyvalue/b Commented Sep 1, 2011 at 3:00

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

He's better off using explode() if its just a plain string... no need for a regex
@shesek: You should expand that into an answer, PHP isn't my native language so I was just going with what was already there.
Thank you for you time my friend, with your answer I could get closer to what I wanted.
0

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

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

1 Comment

Thank you. This is what I'm looking for at the moment.It works great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.