What you need is a program transformation system (PTS).
This is a tool that accepts source-to-source rewrite rules of the general form,
if you see syntax pattern x, replace it by constructor syntax pattern y
that can be applied to structured constructs ("syntax") such as code. Simple string replacement doesn't work for this because it doesn't honor the boundaries of the constructs.
You can sometimes find a PTS that is wired to work on single programming language, such as Spoon (a PTS for legacy Java code). I've never heard of one for PHP.
The really useful ones can be configured to accept the description of a programming language, and provide a PTS for the described language. Stratego, Rascal MPL, and DMS Software Reengineering Toolkit (the commercial PTS I have been working on for 25 years) all do this.
DMS can already be obtained with an appropriate description of PHP and so can be used to accomplish this purpose using DMS's rule specification language.
DMS rules have the general form of
rule rulename ( metavariables ): syntaxcategory -> syntaxcategory
lefthandsidepattern -> righthandsidepattern ;
A simplified version of what you want can be written as a single DMS rewrite rule:
rule insert_entry_probe(name:IDENTIFIER,args:arguments,body:statement_list)
:function-> function
" function \name ( \args ) { \body } "
-> "function \name ( \args ) { log(\"entered\",\makestring\(\name\)) \body } ";
The metaquotes " are used to distinguish code/syntax of the targeted language, from the code/syntax of the rule specification language. name:syntaxtype allows one to specify named metavariables that match complex structures of a particular syntax-type defined by the target grammar. "\" is an escape used inside metaquotes. \name allows one match/use a complex substructures (this is where string matching isnt powerful enough). \name(...) is a metacall to an auxiliary procedure or constructor patterns that computes a complex substructure; in the example, we want to convert the function identifier into a text string. (Forgive my PHP syntax as being rusty).
To handle passing the args to the log function, you'll need more rules to transform the complex structure represented by the args int a list of argument names, with a list of argument values, to be passed to a log function.
Using DMS to process a PHP file, and handing it this rule, will cause it to insert probes tracking function entry in every function found in that file. It should be obvious how to make another rule to track every function exit... but you'll need two rules, one to handle the implicit exit at the end of the function body, and one to handle exits that occur at return statements.
Using such rules, DMS implements test coverage tools for PHP by inserting appropriate instrumentation. That has about 100 rules in it because of the many different bits of PHP syntax that affect control flow and therefore where you want test coverage probes, but one can code those rules from scratch in about a day if you understand how DMS works, and you use the PHP definition as a reference grammar.
A PTS gives you the power to make arbitrarily complex changes to source code.
They are far more powerful than you might guess. We use DMS to do fully automated migrations from million-line mainframe COBOL programs to maintainable Java or C#. No, you can't write the rules for this in an afternoon, but 1-2 engineers can do this in 3-6 months. There's no way to rewrite a million lines of COBOL code by hand in that time frame.