Thread7 Thread7 - 1 year ago 70
PHP Question

How to get rid of "echo" in my PHP code

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download