I want to break a string according to the following rules:
.
) must be treated as one part1
and 2
must be treated as different partsFor example this string:
Method(hierarchy.of.properties) = ?
Should return this array:
Array
(
[0] => Method
[1] => (
[2] => hierarchy.of.properties
[3] => )
[4] => =
[5] => ?
)
I was unsuccessful with preg_split()
, as AFAIK it cannot treat the pattern as an element to be returned.
Any idea for a simple way to do this?
You probably should use preg_match_all over preg_split.
preg_match_all('/[\w|\.]+|[^\w\s]+/', $string, $matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => Method
[1] => (
[2] => hierarchy.of.properties
[3] => )
[4] => =
[5] => ?
)
)