468,242 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

removing a string from a file -- best method

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.

Jun 15 '07 #1
6 1679
Am Fri, 15 Jun 2007 14:00:57 -0000, schrieb la*****@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
Jun 15 '07 #2
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?

Jun 15 '07 #3
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);

Jun 15 '07 #4
On Jun 15, 6:43 pm, lawp...@gmail.com 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?
What is your question, actually? It seems to me your answer is
actually "Can I replace/remove a string without reading/writing the
whole file?" If that's the question, than "No", and that's not
something you are limited with from PHP but from the filesystem.

Jun 15 '07 #5
C.
On 15 Jun, 19:03, Darko <darko.maksimo...@gmail.comwrote:
On Jun 15, 6:43 pm, lawp...@gmail.com 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?

What is your question, actually? It seems to me your answer is
actually "Can I replace/remove a string without reading/writing the
whole file?" If that's the question, than "No", and that's not
something you are limited with from PHP but from the filesystem.

methinks sed might be a better solution than php

C.

Jun 15 '07 #6
On Jun 15, 2:03 pm, Darko <darko.maksimo...@gmail.comwrote:

What is your question, actually? It seems to me your answer is
actually "Can I replace/remove a string without reading/writing the
whole file?" If that's the question, than "No", and that's not
something you are limited with from PHP but from the filesystem.
Without me being aware of it, that was the actual question I was
getting after. Thank you!

Jun 17 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by hokiegal99 | last post: by
6 posts views Thread by rakesh | last post: by
19 posts views Thread by Lionel B | last post: by
23 posts views Thread by Peter Row | last post: by
7 posts views Thread by Simon Hart | last post: by
3 posts views Thread by | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.