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 17:13

On Fri, 15 Jun 2007 16:43:13 +0000, lawpoop wrote:

> And now for the reverse question -
>
> What's the best way to insert a string, or a line, in an arbitrary
> position in a text file?

How is the "arbitrary" position defined?

If it is defined as "always after string XYZ" then you can use the
same approach as in your first question.

Simple read the contents of the file into a string variable and replace
"XYZ" with "XYZ"+newstring.

Example:

$filename = "path/to/file";
$looking_for = "XYZ";
$new_string = "very important stuff";
$string_to_be_inserted = $looking_for . $new_string ;

$content = file_get_contents($filename);

$clean_content = str_replace($looking_for, $string_to_be_inserted"",
$content);

file_put_contents($filename, $clean_content);

 

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

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