LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-20-2010, 06:09 PM   #1
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Rep: Reputation: 30
trying to delete data from a file with php


hey all i've been using php/fwrite to add text to a file using the below code but how do i remove text from a file?

note that i don't know what line it's on as there will be other content in the file

Code:
<?PHP
$File = "textfile.txt";
$fh = fopen($File, 'a');
$Data = "text to put in the text file";
fwrite($fh, $Data);
fclose($fh); 
?>

Last edited by steve51184; 10-20-2010 at 06:11 PM.
 
Old 10-20-2010, 06:31 PM   #2
sleddog
Member
 
Registered: Jan 2002
Location: Labrador, Canada
Distribution: CentOS, Debian
Posts: 182

Rep: Reputation: 35
If it's a relatively small file then you could:

- Read the file content into a variable, using file_get_contents()
- Use str_replace() to delete/replace text in the variable,
- Write the altered content backed to the file using fopen(), fwrite(), fclose() as you did in your example.
 
Old 10-20-2010, 06:32 PM   #3
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
sounds good... can you do me an example please?
 
Old 10-20-2010, 06:49 PM   #4
sleddog
Member
 
Registered: Jan 2002
Location: Labrador, Canada
Distribution: CentOS, Debian
Posts: 182

Rep: Reputation: 35
Code:
<?php
// "textfile.txt" contains the phrase "all that glitters is not gold."
//
$file = "textfile.txt";
$content = file_get_contents($file);
$content = str_replace('not gold', 'shiny', $content);
$fh = fopen($file, 'w');
fwrite($fh, $content);
fclose($fh); 
//
// "textfile.txt" now reads "all that glitters is shiny."
?>
Not tested, but you get the idea

Read up on how each function works in the PHP documentation, e.g., str_replace().
 
Old 10-20-2010, 07:32 PM   #5
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
works perfect thank you
 
Old 04-28-2011, 02:20 AM   #6
athrin
Member
 
Registered: Mar 2011
Posts: 135

Rep: Reputation: 1
Quote:
Originally Posted by sleddog View Post
Code:
<?php
// "textfile.txt" contains the phrase "all that glitters is not gold."
//
$file = "textfile.txt";
$content = file_get_contents($file);
$content = str_replace('not gold', 'shiny', $content);
$fh = fopen($file, 'w');
fwrite($fh, $content);
fclose($fh); 
//
// "textfile.txt" now reads "all that glitters is shiny."
?>
Not tested, but you get the idea

Read up on how each function works in the PHP documentation, e.g., str_replace().
wait what this code means?
Code:
$content = str_replace('not gold', 'shiny', $content);
not gold?? shiny??
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to append data to a file with php/fwrite steve51184 Linux - Server 2 08-16-2010 08:35 PM
How to delete a file with this syntax: index.php\n Procher Linux - Newbie 4 11-07-2009 01:24 PM
How to delete hda1 data & File system gardenair Linux - Newbie 1 03-25-2005 08:00 AM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 03:28 AM
Trouble recognising 'post' data in a php file davee Programming 4 10-20-2003 12:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration