What is the purpose of using preg_split in PHP for splitting a mathematical expression?
When splitting a mathematical expression in PHP, we can use preg_split to separate the different components such as numbers, operators, and parentheses. This function allows us to easily tokenize the expression and work with each element individually, making it easier to evaluate or manipulate the expression programmatically.
$expression = "3 + 4 * (2 - 1)";
$tokens = preg_split('/([\+\-\*\/\(\) ])/', $expression, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($tokens);