473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Deleting lines of text from a file

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.

4 1193
nomad
664 Expert 512MB
Question how are you open the file:
are you making it writing? is the file locked?

nomad
Oct 3 '08 #2
Atli
5,058 Expert 4TB
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
BOMEz
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
5,058 Expert 4TB
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

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

Similar topics

5
by: Nora | last post by:
Hi, I have about 200 xml files which contain one line, that I want to delete. This line is always the last line of the file and it always begins with "<?Pub" Transformations don't work as due...
2
by: GMK | last post by:
Dear all in my asp.net application i have a text file that is installed with my application on the server. this text file is filled with data through a web interface in my application. i need to...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
5
by: George | last post by:
VB.net 2003 standard, XP windows home edition. Installed first application OK today. When I removed the application via Control Panel, there were no problems and the app folders were deleted. ...
2
by: SiouxieQ | last post by:
Hi there, I'm using the code below to try to delete a name from a list of names in a file. Unfortunately it doesn't quite do what I want it to. Instead of looking for the name in the...
14
by: micklee74 | last post by:
hi say i have a text file line1 line2 line3 line4 line5 line6 abc
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
One more for today.... As I add more and more lines to my RichTextBox the array that holds its strings gets bigger and bigger and the vertical scroll bar gets smaller and smaller until the...
8
by: Horacius ReX | last post by:
Hi, I need to write a program which reads an external text file. Each time it reads, then it needs to delete some lines, for instance from second line to 55th line. The file is really big, so...
0
by: aadsaca | last post by:
Deleting/Removing lines on Text File in VB -------------------------------------------------------------------------------- Hi there, i just want to know the syntax on how to remove line on...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.