You could probably do the whole thing in PHP using preg_replace(). You can use find to return a list of suitable file names from a given start point
Code:
find startdir -type -f -print
Perl is another program to consider using as that makes changing files in situ a breeze
Code:
perl -pi.bak -e 's|old string|new/better string|g' file1 file2 ...
will make the changes to the files, preserving the originals with a .bak suffix.
There's More Than One Way To Do It!