Reputation: 1090
I inherited a big php file that I need to fix. It contains over 500 echo statements that the previous developer included to debug his code. I need to find an easy way to wipe out all these echo statements so I don't have all this junk output to the screen. Sometimes he put the echo on the same line as another piece of code.
One thought I had was to create a function called something like "nullo" that does nothing. Then search and replace the word echo with nullo. But the "echo" usually doesn't have a parenthesis. So PHP just gives an error when it encounters a statement like:
nullo 'LABEL: ';
If I need to go through each one and add parenthesis manually, like this:
nullo('LABEL: ');
I might as well just go ahead and delete all 500+ of them instead. The "echo" is also sometimes on the same line as other necessary code. So I cannot just // out all those lines.
Here is a sample of some of the code I am dealing with.
function sample_func(){
global $somevar;
$allnames = "";
echo 'LABEL: '; foreach($somevar as $var2) foreach($var2 as $pickk){ echo ' -' , $pickk[0]->key , '-' , $pickk[1]->name; $allnames=$allnames.$pickk[1]->name;}
return $allnames;
}
I am not against downloading and installing some fancy IDE/notepad program that could do a smart search and replace. Any ideas?
Upvotes: 0
Views: 86
Reputation: 393
use this:
sed '/^[[:blank:]]*echo.*\;*[[:blank:]]*/d;s/^[[:blank:]]*echo.*\;//;s/ echo.*\;//' your_file.ext > new_file.ext
Edited thanks to comment below :)
The final edit also removes lines which only have echo's making the file much cleaner after the edit.
Upvotes: 1
Reputation:
How about this?: (^|(;))\s*echo\s.*;
Catches any echo
that's at the start of a line or preceded by a semicolon, up until the next semicolon. You'd have to backreference with \2
or $2
(whichever your editor uses) to replace any leading semicolons that the regex erased.
Upvotes: 0
Reputation: 1546
Does this work? It uses a possessive quantifier to reduce the runtime of the regex engine since you're finding over 500 instances... let me know if you'd like any additional clarification or functionality.
\becho\b[^;]++;
\b
is a word anchor to make sure that other words like "prechordal"
aren't matched.
Good luck!
Upvotes: 2