With Regular Expressions I’m trying to remove all the methods/functions from the following code. Leaving the “global scope” alone. However, I can’t manage to make it match for all the inner content of a method.
<?php
$mother = new Mother();
class Hello
{
public function FunctionName($value="username",)
{
}
public function ododeqwdo($value='')
{
# code...
}
public function ofdoeqdoq($value='')
{
if(isset($mother)) {
echo $lol;
}
if(lol(9)) {
echo 'lol';
}
}
}
function user()
{
if(isset($mother)) {
echo $lol;
}
if(lol(9)) {
echo 'lol';
}
}
$mother->global();
function asodaosdo() {
}
The current Regular Expression I have is: (?:(public|protected|private|static)\s+)?function\s+\w+\(.*?\)\s+{.*?} However, it won’t select a method that has brackets inside, like function user().
If someone could point me in the right direction.
You can’t do this properly with regex. You need to write a parser that can properly parse comments, string literals and nested brackets.
Regex cannot cope with these cases:
EDIT
Here’s a little demo of how to use the tokenizer function mentioned by XUE Can:
which will print:
which is the original source, only without functions.