vote up 0 vote down
star

I want to use PHP to replace javascript functions in HTML documents. For example:

original:

function my_function(hey) {
    do stuff
}

new:

function new_function(hi) {
    do different stuff
}

I was thinking of using regular expressions with the ereg_replace function, but I'm not sure if this is the best approach. The code in each function is pretty long... plus regex looks very daunting to me at the moment. Any suggestions?

flag
What do you need it for? – Gumbo Jul 10 at 20:27
2 
You'd probably be better off using Notepad++ or some other tool to refactor your code in this manner – John Rasch Jul 10 at 20:27
You're setting yourself up for a lot more work than need be. There should never be a time in you life when you think that writing regex to parse through HTML is a good idea. Ever. It's simply a waste of time. Go with what Josn Rasch said... use an editor and do a find and replace. – KyleFarris Jul 11 at 0:04

2 Answers

vote up 1 vote down

Looks like you trying to do something pointless. Put a new function and call it. Just do no use the old one, once you anyway want to replace the entire body with it's name.

link|flag
vote up 0 vote down

This is the kind of thing that perl is built for. Spend a day learning how to slurp in files and take a good look at the many regex tools out there, and you'll have yourself a script that you can modify to use in the future, if need be, but is otherwise throwaway, which is good, since you probably wrote it crappily, being your first one and all.

link|flag

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.