2

I'm editing a PHP file to replace a function's code by another code provided. My problem is that the function's code may change. So, I don't know exactly how I should do the regex to find it.

Here's an example:

function lol()
{
    echo('omg');
    $num=0;
    while($num<100)
    {
        echo('ok');
    }
}
function teste()
{
    echo('ola');
    while($num<100)
    {
        echo('ok');
    }
}
function test()
{
    echo('oi');
    while($num<100)
    {
        echo('ok');
    }
}

What I need is to get the code of function teste(), which would be:

echo('ola');
while($num<100)
{
    echo('ok');
}

BUT: I do not know how many whiles, foreaches or how many brackets are inside the functions, neither I don't know it's order. The example was just an example.

How would I be able to get the function's code? Thank you in advance.

10
  • What is your final goal with this? There may be a better way of doing what you want Commented Jun 25, 2013 at 8:05
  • 1
    I wouldn't even try to parse PHP with regex. Commented Jun 25, 2013 at 8:05
  • do you need EVERYTHING is inside the function teste() or just the first line? Commented Jun 25, 2013 at 8:05
  • That's not the code for function teste Commented Jun 25, 2013 at 8:07
  • Do you want the full content of your function of just the echo? Commented Jun 25, 2013 at 8:10

2 Answers 2

2

Disclaimer

As other users stated, this is probably not a good approach. And if you decided to use it anyway, double-check your result as regex can be treacherous.

Ugly solution

You can you something like to match even if the function is the last one (@Nippey):

(?:function teste\(\))[^{]*{(.*?)}[^}]*?(?:function|\Z) 

Note: I'm using (?:xyz) which is for non-capturing parentheses group. Check if your regex engine support it.

Output

    echo('ola');
    while($num<100)
    {
        echo('ok');
    }
Sign up to request clarification or add additional context in comments.

5 Comments

I know you explicitly stated that this works if teste is not the last function... but what happens if it is?
You could use this modification, so you don't have to remove the brackets: (?:function teste\(\))[^{]*{(.*?)}[^}]*?(?:function)
@STTLCU: Use this: (?:function teste\(\))[^{]*{(.*?)}[^}]*?(?:function|\Z)
@EdouardLopez i'm not OP but i guess that if you used nippey's suggestion now the output is without external braces
I used the first option, where function is not the last function, as I know it isn't and won't be in the platform which with I'm working for. However, this last one is better, so thank you both of you :)
1

Using recursive pattern (?R) you can match nested brackets:

$result = preg_replace_callback('#(.*?)\{((?:[^{}]|(?R))*)\}|.*#si', function($m){
    if(isset($m[1], $m[2]) && preg_match('#function\s+teste\b#i', $m[1])){
        return $m[2];
    }
}, $code); // Anonymous function is used here which means PHP 5.3 is required

echo $result;

Online demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.