Here is something to start with:
$file = 'somefile.php';
$f = fopen($file,'r');
$temp = dirname($file).'/temp.php';
print_r($temp);
$t = fopen($temp, 'w');
$mode = 'write';
$buffer = [];
while($line=fgets($f)){
print_r($mode."
");
switch($mode){
case 'write':
if(preg_match('/^\s*\/\\*/', $line)){
$buffer[] = $line;
$mode = 'comment';
}else{
fputs($t,$line);
}
break;
case 'comment':
if(preg_match('/^\s*\\**\s*@TODO/i', $line)){
$buffer = [];
$mode = 'skip';
}else if(preg_match('/^\s*\\*\//', $line)){
fwrite($t,implode("", $buffer));
$buffer = [];
fputs($t,$line);
$mode = 'write';
}else{
$buffer[] = $line;
}
break;
case 'skip':
if(preg_match('/^\s*\\*\//', $line)){
$mode = 'write';
}
break;
}
}
/*
fclose($t);
fclose($f);
rename($temp, $file);
*/
I tested it against this file
<?php
$var= "foo";
/*
@todo hello
some other shuff
*/
/**
*
* @author Me
*
*/
class someclass{
}
This was the stuff in temp.php
<?php
$var= "foo";
/**
*
* @author Me
*
*/
class someclass{
}
hope it helps. I would probably back up your files before attempting this.
UPDATE
Had to fix a couple minor things so it would skip comments without @todo
. The rest you can probably work out fairly easily.
The regex is only there to account for spaces or comments that are like this
/*
* @todo
*/
This \\*
becomes \*
for the regex engine that then becomes a literal *
.
So this one \s*\/\\*
-
\s*
- none or more spaces
-
\/
- the /
literally
-
\\*
- the *
literally
Or in English the /*
open comment tag.
I mention this because the regex is tricky, but you probably have spaces and other stuff in there, probably every @todo
isn't the very first thing on the line.
Doing it like this, you get to handle each line on it's own, it simplifies things and more importantly increases the matching accuracy.