0

I have a string of below type.

[test1,3,(257,305,435,900)]

Now i want to split this string into array cell. like this one

term => 'test1', 
num => 3,
info => array{ [0]=> 275, [1]=> 435, [2]=> 900 }

can any one know a regular expression pattern for this string....thanks in advance.

1 Answer 1

4

You probably want to use preg_match_all.

$string = "[test1,3,(257,305,435,900)]";

preg_match_all('/\[(?P<term>\w+),(?P<num>\d+),\((?P<info>[\d,]+)\)\]/', $string, $out);

$results = array (
   'term' => $out['term'][0],
   'num'  => $out['num'][0],
   'info' => explode(',', $out['info'][0])
);

var_dump($results);
Sign up to request clarification or add additional context in comments.

6 Comments

NB: split() emits E_DEPRECATED, use explode() instead. But +1, this works nicely.
Thanks @DaveRandom. Why is split() deprecated?
Thank your for your quick and accurate response, but actually my string data is in this form.....[test1,3,(257,305,435,900)] OR [test1,1,(673)] OR [test1,1,()].....i.e: want that regx work on this form.....once again thanks.
Because it is part of the ereg family of functions. I actually thought it was just an alias of explode() until I looked it up in the manual but it seems this is not the case - I guess that comes from the fact that join() is an alias of implode(), and when you also write in Javascript it is easy to get them the wrong way round. I only discovered this myself when I ran your code and saw the error.
Now i fix this one...../[(?P<term>\w+),(?P<num>\d+),((?P<info>[\d,]*))]/..... Only i change the last + to *.....hurah working perfect....
|

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.