From a04f38c2625deb7e26a52147c6e8fd45581cffe2 Mon Sep 17 00:00:00 2001 From: Tobsn Date: Wed, 27 Jun 2012 03:46:31 -0700 Subject: [PATCH] Now with Balanced Parentheses! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit it's not regex, its not posix, but it can fly… finally balanced parentheses to fix all the () issue. it's ugly but it's fast and works with all kinds of combinations. it basically just counts open vs. closed and if they are even again it knows exactly where the condition is and does not cut into html or cut off too much. --- laravel/blade.php | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/laravel/blade.php b/laravel/blade.php index 7e4e2fdb..f224614a 100644 --- a/laravel/blade.php +++ b/laravel/blade.php @@ -260,9 +260,42 @@ protected static function compile_endforelse($value) */ protected static function compile_structure_openings($value) { - $pattern = '/@(if|elseif|foreach|for|while)\s*?(\(.+?\))/'; - - return preg_replace($pattern, '', $value); + preg_replace_callback( + '/@(if|elseif|foreach|for|while)(\s*?)(\([^\n\r\t]+\))/', + function($matches) use (&$value) + { + if(count( $matches ) === 4) + { + $open = 0; + $close = 0; + $cut = 0; + $len = strlen($matches[3]); + for($i = 0; $i < $len; $i++) + { + if($matches[3][$i] === '(' ) + { + $open++; + } + if($matches[3][$i] === ')' ) + { + $close++; + } + if($open !== 0 && ($open === $close)) + { + break; + } + } + $condition = substr($matches[3], 0, ($i + 1)); + $value = str_replace( + '@'.$matches[1].$matches[2].$condition, + '', + $value + ); + } + }, + $value + ); + return $value; } /**