You are here: Re: removing a string from a file -- best method « PHP Programming Language « IT news, forums, messages
Re: removing a string from a file -- best method

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация