I need do it, basically:
Parameter | Expected Result (array)
a | [a]
a_b | [a, b]
a_b_c | [a, b, c]
a__b | [a_b] <-- note that the double underline be unique
a_b__c | [a, b_c]
I know how I do it with the methods explode and str_replace and using a foreach to converts __ to _. Basically this:
<?php
$parameter = 'a_b__c';
$expected = str_replace( '__', "\0", $parameter ); # "a_b\0c"
$expected = explode( '_', $expected ); # ["a", "b\0c"]
foreach ( $expected as &$item )
$item = str_replace( "\0", '_', $item );
# ["a", "b_c"]
?>
But I guess that with preg_* it can be more fast. Or am I wrong?
Well, I accept any better suggestion. 🙂
Help note: the $parameter will be ever a PHP identifier (generally a class identifier).
You can try the following approach using preg_split:
?<!and?!are zero-width negative lookaround assertions.