Current location: Hot Scripts Forums » Programming Languages » PHP » how to delete particular block of text from a text file?


how to delete particular block of text from a text file?

Reply
  #1 (permalink)  
Old 05-28-09, 12:20 AM
umarmir_123 umarmir_123 is offline
Newbie Coder
 
Join Date: May 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
how to delete particular block of text from a text file?

hi,
i want to search for a particular string in a file and then want to delete the following text
till this particular string is again encountered.my file is like this
text.txt
mail
subject:-hello
email:-what is ur name?
where do u live?ect
mail
subject:-morning
email:-come in the morning.

mail
subject:-ello
email:-what is ur fathers name?
where do u live?ect
now suppose i want to delete from the second occcurence of mail i.e
my updated file shud be like this
text.txt
mail
subject:-hello
email:-what is ur name?
where do u live?ect
mail
subject:-ello
email:-what is ur fathers name?
where do u live?ect

i hope you understood my problem.if anybody know plz help me.
if you need any further info plz let me know.
thnx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-28-09, 03:51 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You can try this function:
PHP Code:

<?php
function delete_file_entry($file,$seperator,$entry)
{
 
$regx_seperator "/".$seperator."\s/";
 
$text file_get_contents($file);
 
$stringParts preg_split($regx_seperator,$text);
 for(
$i=1;$i<count($stringParts);$i++)
 {
  if(
$i != $entry){$newText .= $seperator.$stringParts[$i];}
  }
 
$fh fopen($file'w');
 
fwrite($fh,$newText);
 
fclose($fh);
 
 
// This line of code is optional. Leave in if you want to see the results.//
 
echo "<b><u>Old text :</u></b><p>".nl2br($text)."<p><b><u>New text :</u></b><p>".nl2br($newText);
 }
 
 
 
// Usage: Delete second mail entry. //
delete_file_entry("text.txt","mail",2);
?>
text.txt
Code:
mail
subject:-hello
email:-what is ur name?
where do u live?ect
mail
subject:-morning
email:-come in the morning.
mail
subject:-ello
email:-what is ur fathers name?
where do u live?ect
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-29-09, 01:22 AM
umarmir_123 umarmir_123 is offline
Newbie Coder
 
Join Date: May 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
thnx for reply but i am encountering an error

i am encountering following error message
Fatal error: Call to undefined function: file_get_contents() in c:\apache\htdocs\ims\delete.php on line 24
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-29-09, 08:13 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by umarmir_123 View Post
i am encountering following error message
Fatal error: Call to undefined function: file_get_contents() in c:\apache\htdocs\ims\delete.php on line 24
That's because your hosting account doesn't have PHP 5.
You need to tell your hosting provider to get with the program and upgrade to the latest version of PHP.
It's FREE!!!

In the mean time, this will work with earlier versions of PHP.
PHP Code:

<?php
function delete_file_entry($file,$seperator,$entry)
{
 
$regx_seperator "/".$seperator."\s/";
 
$fh fopen($file"r");
 while(!
feof($fh)){$text .= fgets($fh);}
 
fclose($fh);
 
$stringParts preg_split($regx_seperator,$text);
 for(
$i=1;$i<count($stringParts);$i++)
 {
  if(
$i != $entry){$newText .= $seperator.$stringParts[$i];}
  }
 
$fh fopen($file'w');
 
fwrite($fh,$newText);
 
fclose($fh);
 
 
// This line of code is optional. Leave in if you want to see the results.//
 
echo "<b><u>Old text :</u></b><p>".nl2br($text)."<p><b><u>New text :</u></b><p>".nl2br($newText);
 }
 
 
 
// Usage: Delete second mail entry. //
delete_file_entry("text.txt","mail",2);
?>
__________________
Jerry Broughton

Last edited by job0107; 05-29-09 at 08:16 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-31-09, 12:11 PM
DionDeVille DionDeVille is offline
New Member
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How would the PHP5 script be adapted to find and replace one string with another?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-01-09, 07:11 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by DionDeVille View Post
How would the PHP5 script be adapted to find and replace one string with another?
You need to examine the code I already wrote, until you understand what it does.

If I keep writing the code for you, you aren't going to learn anything.

And if it's your intention to not learn, but have someone write the code for you,
then I charge $25.00 per hour to write what ever code you want.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-04-09, 01:46 AM
umarmir_123 umarmir_123 is offline
Newbie Coder
 
Join Date: May 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
You need to examine the code I already wrote, until you understand what it does.

If I keep writing the code for you, you aren't going to learn anything.

And if it's your intention to not learn, but have someone write the code for you,
then I charge $25.00 per hour to write what ever code you want.
thnx for ur help but their is a bit prblm . After deletion it mixup "mail" string with other file data. So any idea about it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
text box with scroll bar silvermane CSS 7 01-16-09 04:03 AM
writing form data to a text file topside Script Requests 2 03-02-07 03:28 AM
Draggable Tables Ares JavaScript 10 08-03-06 07:55 AM
i know how to delete a file! but how to delete a directory? forcer JavaScript 3 01-28-04 08:00 AM
Writes to a text file gamextremer2003 JavaScript 4 09-11-03 10:43 AM


All times are GMT -5. The time now is 09:24 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.