Home  >  Article  >  Backend Development  >  A simple example of PHP judging whether the brackets in the expression match_php example

A simple example of PHP judging whether the brackets in the expression match_php example

WBOY
WBOYOriginal
2016-12-05 13:28:171194browse

looks like this:

<&#63;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 &#63; "表达式匹配" : "缺少右括号)"; 
} 
$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); 
&#63;> 

Page information:

缺少右括号) 
表达式匹配 
缺少左括号(  

The above is the simple example that the editor brought to you in PHP to determine whether the brackets in the expression match. I hope you will support Script Home~

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn