<?php
function braceCheck(array $values) : array
{
for ($i = 0; $i <= count($values) - 1; $i++) {
$braces = str_split($values[$i]);
$values[$i] = isValid($braces);
}
return $values;
}
function isValid(array $braces) : string
{
$available = ['{' => '}', '(' => ')', '[' => ']'];
$opened = [];
$closed = [];
for ($i = 0; $i <= count($braces) - 1; $i++) {
if (in_array($braces[$i], array_keys($available))) {
$opened[] = $braces[$i];
continue;
}
$closed[] = $braces[$i];
}
return match($braces, $available, $opened, $closed);
}
function match($braces, array $available, array $opened, array $closed) : string
{
$last_open = $opened[count($opened) - 1];
$opened = array_values($opened);
$closed = array_values($closed);
if(!testValidity($braces, $available, $last_open, $opened, $closed)) {
return 'NO';
}
if (count($opened) > 1) {
$result = match(
$braces,
$available,
array_slice($opened, 0, -1),
array_slice($closed, 1)
);
}
return 'YES';
}
function testValidity($braces, array $available, string $last_open, array $opened, array $closed) : bool
{
$position = strpos($closed[0], $braces);
echo $position;
$arr = array_slice($braces, 0, $position);
$flipped = array_flip($available);
vardump($arr, $flipped);
in_array($arr, $flipped[$closed[0]]);
// check if not nested properly
if ($available[$last_open] !== $closed[0]) {
// check if the brackets are consecutive
if ($available[$opened[0]] === $closed[0]) {
return true;
}
return false;
}
// check if the brackets are not consecutive
if ($available[$opened[0]] !== $closed[0]) {
// check if nested properly
if ($available[$last_open] === $closed[0]) {
return true;
}
return false;
}
return true;
}
$values = ['{[()]}', '{[(]}', '{}()[]', '{]}[']; // YES, NO, YES, NO
var_dump(braceCheck($values));
?>
- Output for git.master_jit, git.master, rfc.property-hooks
- Parse error: syntax error, unexpected token "," in /in/VXq5d on line 26
Process exited with code 255.
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
52.88 ms | 401 KiB | 8 Q