Skip to content

Instantly share code, notes, and snippets.

@anjanesh
Last active Feb 1, 2020
Embed
What would you like to do?
<?php
$s = "((a+b*d)*(x/2))*(1+(y+(x-2)/10))";
$b = [];
$j = 0;
$stack = [];
for ($i = 0; $i < strlen($s); $i++)
{
if ($s[$i] == '(')
{
$b[++$j] = '';
}
elseif ($s[$i] == ')')
{
$stack[] = ($b[$j--] .= ')');
}
for ($k = 1; $k <= $j; $k++)
{
$b[$k] .= $s[$i];
}
}
echo $s.PHP_EOL;
print_r($stack);
echo PHP_EOL;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment