To modify source code reliably, you pretty much need to be able to parse it the same way a compiler does, point at language entities using the standard scoping rules for that language, and state your changes to the code. Tools that do this are called program transformation engines.
To provide all this compiler machinery integrated with production quality language front ends is considerable work so there aren't a lot of these tools around.
See our DMS Software Reengineering Toolkit. This is a program transformation engine that handles multiple languages (C, C++, C#, Java, PHP, JavaScript, ...) and enables this kind of work. A nice property of a source-to-source style transformation engine like DMS is the ability to write transformations using the (surface) syntax of the langauge(s) you want to manipulate, rather than more traditional pure-compiler like tools that insist you simply hack at an abstract syntax tree with lots of procedural code.
It isn't as easy is "oh, just change this". But is it reliable.
EDIT 6/23/2011: OP said it had to work with Java and PHP. DMS does.