|
Posted by Christian Aigner on 06/15/07 14:56
Am Fri, 15 Jun 2007 14:00:57 -0000, schrieb lawpoop@gmail.com:
> Hello all -
>
> I have a situation where I want to remove a string from a file and
> write it to disk before I do further work with it. It's a malformed
> xlm document with a string that causes our parsing to choke. I know
> exactly where the string occurs, and exactly what it is.
>
> So what is the best method to remove the string from the file? I'm
> used to working with an array of each file line from the file()
> function, but that seems like it might be overkill in this situation.
You could use file_get_contents to read the entire file into a string,
then perform a str_replace to replace the unwanted part of the string
with "". Then write the content back into the file.
$filename = "path/to/file";
$unwanted = "some unwanted string";
$content = file_get_contents($filename);
$clean_content = str_replace($unwanted, "", $content);
file_put_contents($filename, $clean_content);
Regards,
Christian
Navigation:
[Reply to this message]
|