Вы можете попробовать вот так - на основе ответа в другом месте в стеке .Модифицировал шаблон и добавил preg_replace, чтобы на результаты не влияли пробелы во входной строке.
<code>$input = '2 + 3 * 7';
$input = '2-5/3.4';
$pttn='@([-/+\*])@';
$out=preg_split( $pttn, preg_replace( '@\s@', '', $input ), -1, PREG_SPLIT_DELIM_CAPTURE );
printf('<pre>%s
', print_r ($ out, true));
Будет выводить:
Array
(
[0] => 2
[1] => -
[2] => 5
[3] => /
[4] => 3.4
)
Обновление:
<code>$input = '2 + 5 - 4 / 2.6';
$pttn='+-/*'; # standard mathematical operators
$pttn=sprintf( '@([%s])@', preg_quote( $pttn ) ); # an escaped/quoted pattern
$out=preg_split( $pttn, preg_replace( '@\s@', '', $input ), -1, PREG_SPLIT_DELIM_CAPTURE );
printf('<pre>%s
', print_r ($ out, true));
выходы:
Array
(
[0] => 2
[1] => +
[2] => 5
[3] => -
[4] => 4
[5] => /
[6] => 2.6
)