By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Deleting lines of text from a file

P: 40
Hi all,

I'm just trying to delete some lines of text from a text file that I open with fopen.

Based on search results I've tried using
Expand|Select|Wrap|Line Numbers
  1. str_replace($_POST['mySearchString'], "", fgets($openedFile));
This gives no result
I've also tried it as :

Expand|Select|Wrap|Line Numbers
  1. str_replace($_POST['mySearchString'], "", $openedFile);
Where am I going wrong?
Oct 3 '08 #1

✓ answered by Atli

I don't need a return value, I'm just trying to get some lines of text out of a text file.
Whether or not you need a return value is irrelevant.

Like I explained in my previous post, the str_replace function returns the altered string. It does not alter the strings you pass into it.
Therefore, if you want to change a string, you need to use the return value of the str_replace function, or the call is completely pointless.

In your case, however, the str_replace function isn't that useful.
I would just use the fgets function to copy a file line by line, skipping the lines that you want excluded.

Share this Question
Share on Google+
4 Replies


nomad
Expert 100+
P: 664
Question how are you open the file:
are you making it writing? is the file locked?

nomad
Oct 3 '08 #2

Atli
Expert 5K+
P: 5,058
What are you doing with the return value of the function?

The str_replace function, like most of the string functions, doesn't alter the variables you pass into it, but rather uses those variables to create a new one, which it returns.

So, if you did:
Expand|Select|Wrap|Line Numbers
  1. $text = "I like oranges.";
  2. $output = str_replace("oranges", "apples", $text);
  3. echo $text;
  4.  
It would still output "I like oranges."

The altered version "I like apples" would be in the $output variable.
Oct 4 '08 #3

P: 40
What are you doing with the return value of the function?

The str_replace function, like most of the string functions, doesn't alter the variables you pass into it, but rather uses those variables to create a new one, which it returns.

So, if you did:
Expand|Select|Wrap|Line Numbers
  1. $text = "I like oranges.";
  2. $output = str_replace("oranges", "apples", $text);
  3. echo $text;
  4.  
It would still output "I like oranges."

The altered version "I like apples" would be in the $output variable.
I don't need a return value, I'm just trying to get some lines of text out of a text file. For example if I have :

Cat1
Cat2
Cat3
Cat4
Cat5
and I want to get rid of Cat 3 from the text file replacing it with:

Cat1
Cat2
Cat4
Cat5
additionally I am opening the file with "r+" permissions.
Oct 6 '08 #4

Atli
Expert 5K+
P: 5,058
I don't need a return value, I'm just trying to get some lines of text out of a text file.
Whether or not you need a return value is irrelevant.

Like I explained in my previous post, the str_replace function returns the altered string. It does not alter the strings you pass into it.
Therefore, if you want to change a string, you need to use the return value of the str_replace function, or the call is completely pointless.

In your case, however, the str_replace function isn't that useful.
I would just use the fgets function to copy a file line by line, skipping the lines that you want excluded.
Oct 6 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.