PHP判断表达式中括号是否匹配的简单实例
如下所示:
<?php /** * title: 判断表达式中括号是否匹配 * Description: () 匹配 )(不匹配 利用压栈和出栈 * @author Mr Lv */ function isValid($expstr) { $temp = array(); for ($i=0; $i<strlen($expstr); $i++) { $ch = $expstr[$i]; switch($ch) { case '(': array_push($temp, '('); break; case ')': if (empty($temp) || array_pop($temp) != '(') { return "缺少左括号("; } } } return empty($temp) == true ? "表达式匹配" : "缺少右括号)"; } $expstrA = "(1+3(6*4)-(2+3))()("; $expstrB = "(1+3(6*4)-(2+3))()"; $expstrC = "(1+3(6*4)-(2+3)))"; echo isValid($expstrA); echo "<br>"; echo isValid($expstrB); echo "<br>"; echo isValid($expstrC); ?>
页面信息:
缺少右括号) 表达式匹配 缺少左括号(
相关推荐
qidu 2020-05-15
jyj00 2019-12-15
wangzhaotongalex 2015-01-07
peierlsfox 2016-07-19
景泽元的编程 2020-06-21
lancanfei 2019-06-26
83433860 2016-09-26
zheglei 2016-01-14
BlowfishKing 2018-06-01
lingxian 2018-06-01
bianhuakairi 2019-05-05
liuxiang 2011-09-03
chengcuotuo 2019-04-07
Passerby 2019-04-03