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 - str_replace($_POST['mySearchString'], "", fgets($openedFile));
This gives no result
I've also tried it as : - str_replace($_POST['mySearchString'], "", $openedFile);
Where am I going wrong?
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
Question how are you open the file:
are you making it writing? is the file locked?
nomad
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: -
$text = "I like oranges.";
-
$output = str_replace("oranges", "apples", $text);
-
echo $text;
-
It would still output "I like oranges."
The altered version "I like apples" would be in the $output variable.
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: -
$text = "I like oranges.";
-
$output = str_replace("oranges", "apples", $text);
-
echo $text;
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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.
...
|
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...
|
by: micklee74 |
last post by:
hi
say i have a text file
line1
line2
line3
line4
line5
line6
abc
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |