if (preg_match('/^ ( (?: [^()]++ | \( (?1) \) )* ) $/x', $text)) echo "text is balanced\n"; else echo "text is unbalanced\n"; ----------------------------------------------------------------------------- Copyright 1997-2024 Jeffrey Friedl