Possible Duplicate:
Retrieving numeric characters from a string
I have a string like this –
[ [ -2, 1/2 ], // 1/2 represents one half
I want to retrieve the numeric characters and put them into an array that will end up looking like this:
array(
[0] => -2,
[1] => 1/2
)
What is the best way of doing this?
A more thorough example –
[ [ -2, 1/2, 4, 8 ],
[ 5, 1/2, 1, -4/3 ],
[ -2, 7/2, 4, 4 ],
[ -2, 1/2, -3, 2 ] ]
I am going through this matrix line by line and I want to extract the numbers into an array for each line.
This question is very similar to a question I asked before – Retrieving numeric characters from a string
The difference is that there are no decimal points in the numbers. Instead non-integers are represented as fractions.
Try to deal with preg syntax. Your preg expression is simple:
/(-?\d+(?:\/\d+)?)/.