Created
August 7, 2018 11:46
-
-
Save AlexMcowkin/1e36d001931c690a691896315ad233e1 to your computer and use it in GitHub Desktop.
Проверка строки на верное количество скобок на php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$string = 'This is (ok)'; | |
function bracketValidation($string) | |
{ | |
$counter = 0; | |
$openBracket = ['(','{','[']; | |
$closedBracket = [')','}',']']; | |
$length = strlen($string); | |
for($i = 0; $i<$length; $i++) | |
{ | |
$char = $string[$i]; | |
if(in_array($char, $openBracket)) | |
{ | |
$counter ++; | |
} | |
elseif(in_array($char, $closedBracket)) | |
{ | |
$counter --; | |
} | |
} | |
if($counter != 0) | |
{ | |
return false; | |
} | |
return true; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Иначе будет ошибка в случае:
}{
)(