You are not logged in.
#77 27 Jan 2008 1:07 pm
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Ok. Would it be possible to add a die('Could not replace VARIABLE_NAME with VARIABLE_NAME') or similiar at the end of the str_replace functions? Cause right now, I have no way of telling which wrote and which did not, other than opening the files and checking each spot where it should have done it to a other version of the file where it did it.
Offline
#79 30 Jan 2008 7:03 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
I mean like after each str_replace, I have a set of them for each file that I run to change different locations in each file. But some go wrong and some don't, so I really need a way of seeing which went wrong and which did not.
Offline
#80 31 Jan 2008 9:15 am
Re: Automated File Changes
I'd probably create my own replace method that returned true or false depending on whether it replace what it needed to replace
something like:
Code:
function my_replace($search, $replace, $subject, &$return) { $orig = $subject; $return = str_replace($search, $replace, $subject); if($orig === $return) return false; return true; }
so you could loop over all the changes you need to make, and if my_replace returns false, you break out of the loop
Code:
$file_contents = file_get_contents($file); foreach($changes as $find=>$replace) { $change = $file_contents; if(my_replace($find, $replace, $file_contents, $change)) { echo "Failed to replace '$find' with '$replace'"; break; // can also use exit() if you want to stop right here. die() sort of does the same thing, but exit is what you really need } }
you could also spit out info on what's going on this way too:
Code:
$file_contents = file_get_contents($file); foreach($changes as $find=>$replace) { $change = $file_contents; if(!my_replace($find, $replace, $file_contents, $change)) { echo "Failed to replace '$find' with '$replace'"; echo "In file $file<br/>"; echo "file dump <blockquote><pre>$file_contents</pre></blockquote>"; echo "please copy the above info and email it to me at <a href="mailto:butcher@yourdomain.com">butcher@yourdomain.com</a><br/>"; break; // can also use exit() if you want to stop right here. die() sort of does the same thing, but exit is what you really need }else { echo "Changed '$find' to '$replace'<br/>"; $file_contents = $change; } }
and if something goes wrong, you'll have a full dump of what did happen and what went wrong.
in case you didn't catch what my_replace is doing, its wrapping the str_replace method. it saves the original string passed in to a temp variable (for comparing later), and takes a 4th parameter $return which gets passed by reference, meaning that after the my_replaced is called, whatever variable you pass into my_return @ the $return variable will contain the replaced string value. the method itself returns true/false (true = replaced correctly, false = strings were identical after the str_replace and therefore failed) so that you can know whether or not the operation was successful. the looping code example simply spits out what was replaced on each variable replacement, and if it fails, it spits out what was not replaced as well as the current contents of the file and a note to the user to copy / paste in the above data & email it to you (though if they're using IE unix newlines will be replaced with windows newlines) to give you something to go by and look at when something goes wrong.
Offline
#81 31 Jan 2008 1:35 pm
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
I understand what you mean, I'll see how to best implement it. Will probably have to clean up my current stuff though, it is hell searching through it all to find what I am looking to fix.
Offline