Reputation: 5
exapmle
function hello(){
echo 'hello';
foreach (array(1,2,3) as $n)
{
echo $n;
}
echo 'bye-bye';
echo 'bye-bye';
echo 'bye-bye';
}
function hi(){}
I need everything that is between outer curly brackets, ignoring any inner curly brackets, so my result wont be like that
{
echo 'hello';
foreach (array(1,2,3) as $n)
{
echo $n;
}
or
{
echo 'hello';
foreach (array(1,2,3) as $n)
{
echo $n;
}
echo 'bye-bye';
echo 'bye-bye';
echo 'bye-bye';
}
function hi(){}
Upvotes: 0
Views: 388
Reputation: 437336
In the general case this is not doable with regular expressions. You will need to use a parser designed for the task, or write one of your own.
For basic functionality you don't need much: read characters one at a time, keep a count of how many unclosed braces you have at any one point and consider parsing complete when this number decreases from 1 to 0.
The next step would be to recognize common string literals (single and double quoted) and correctly skip any literal braces in those strings, which would be enough to get you 90% of the way.
Update: here's an asnwer to a similar problem that also includes sample code (although it's in C# instead of PHP).
Upvotes: 1