Reputation:
I have a string like this: x+1=13-x
What I have to do, is separate the unknown (x
) from mathematical symbols (+
, -
, *
, :
or /
) and numbers. That means that the left side of the string would be an array like [x,+,1]
and the right side: [13,-,x]
.
Before this, I was only working with one-digit numbers, so, I just used str_split()
function, but, now I would like to use RegEx which I am not good at.
Upvotes: 3
Views: 183
Reputation: 206699
You'll need to adapt this a bit, but try this:
<?php
$data = "a+123=x-23";
$arr = preg_split("/\\b/", $data);
print_r($arr);
?>
$ php t.php
Array
(
[0] =>
[1] => a
[2] => +
[3] => 123
[4] => =
[5] => x
[6] => -
[7] => 23
[8] =>
)
As you can see, the individual components are visible in the resulting array. You'll need to take care of whitespace issues.
Upvotes: 3