Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Delete Entire Line In Text File If Anything In The Line Matches Search String


how would I code a php script that would open a text file, look for a string i specified, and if it finds that string, it will delete the entire line in the text file that contained that text. I'm new to php, so sorry if this is really obvious..




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
How To Return Just First-line From A Multi-line String Of Text?
I've been looking through the manual for string commands, and I haven't had success in determining the proper call for this, if there is one:

Let's say I have a text string with hard-carriage returns in it:

"The lazy brown fox ran down the road
to the store and bought some eggs
to bake a cake and serve dinner
for all his friends."

So if that string value is in "x", how can I tell it to return JUST the first line: "The lazy brown fox ran down the road"

Read Text File Line By Line
I have been given a textfile which contains data on 1000 businesses.  Each line contains a name and a code which are seperated by a comma.  The code below allows me to display the entire textfile but I want to read in the values from each line and store these in a database.

$filename = "businesses.txt";
$fileToOpen = fopen($filename,"r");
$content = fread($fileToOpen, filesize($filename));
$content = nl2br($content);
fclose($fileToOpen);
echo($content);

Does anyone know how I can read the data in line by line so that I can store it in a database.

How To Delete Last Line Of File
I have a flat file on the server which I append to from a HTML web form.
I need to delete the last line of the flat file before I append though.

How do i do that?

I have the following code. I'm not sure if it's correct.

$pattern = "</member>"; // what I want to look for.
$ora_books = preg_grep($pattern, file('/path/to/your/file.txt'));

PRINTing A Large String – Line By Line Or In One Go?
In many of my apps I read in a HTML file line by line, do some string
substitutions and then print out the file line by line to the browser,
for example:

$fragment_to_show_handle = @fopen("page.html", "r");

while ( !feof($fragment_to_show_handle) ) // while not end of file
{
$line = fgets($fragment_to_show_handle, 1000);

$line = str_replace("%pics%", $pics, $line); // do the
substitutions one at a time
$line = str_replace("%currency_select%", $currency_select, $line);
// etc
print "$line";// print the line to browser
}

fclose($fragment_to_show_handle);

Alternatively, in another app I have done this:

$home_html = implode ('', file("$fragments/home.html"));

$home_html= str_replace("%applet_code%",$applet_code,$home_html);
$home_html=str_replace("%pics%",$pics,$home_html);
// etc

print "$home_html";

Assuming the total output to the browser is the same, say about
20-50k, which approach is more efficient in a php/apache/unix
environment in terms of speed and least server load?

Search File For Line And Replace With Update
I am trying to search through a file for a line. My line that I will be searching is in a cron flat file. I need to search that file for a line simirlar to:

59 23 * * * wget -q http://www.site.com/runPage.php?usr=name

When the line is found, I need to make changes to it. I have read the file into a variable with fread, is that the best way to get this information for the type of search and replace I am trying to do?

From what I have read in past posts, it seems like strstr is a quick way to go or would strpos be? Or, do I have to do more than that?

Open File, Add Line At Beginning, Remove Last Line
I'm trying to open a file, write one line at the very beginning, and delete the last line.

Right now I'm stuck at trying to open the file and write a line at the very beginning. Here's my code:

$f="myfile.txt";
fwrite(fopen($f,"a"),"Add This Line");

I know I'm opening this file in append mode, where it will always write my text at the end of the document, but I can't figure out any other mode that would allow me to open an existing document and not delete everything that is already in it.

New Line In Text File
I'm trying to create a text file in php to generate a playlist. The problem is that I cannot manage to create a new line in the text file. Using /n still places text in the same line and just inserts a symbol to indicate the 'new line'. Using

just works when viewing the document in HTML (which is not what i need in this case). But what I want is to physically insert a new line in the text file so that when i open the generated file using notepad i see that there are actually new lines in the document and so that the playlist can be read normally with a music player.

How Can A Line Be Deleted From A Text File
Since, I cannot seem to delete the last line in a text database using the code below.

What other way can I delete a line from a text file?

How To Edit A Line In A Text File
i have a text file (just testing php) which stores about 10 lines where each line represents a user request. how do i edit that line to allow the user to edit the $bookwanted ? the lines below shows my display info for the user.

$file=file('logindata.txt');

foreach($file as $line){
list($bookwanted,,$user,$pass,$acct,$email,$amt,$date)=explode('|',$line);

if (($username == $user) && ($password == $pass) ) {

// display info for that particular user

}

Random Line From Text File
I want to grab a random line from a flat file and then echo it. I could probably do this but the alcohol prevents me from thinking.

Read Line From Text File
I have a text file that i would like to use PHP to read from. each line
ends with a. I need to read from the beginning of the line, only upto that:

right now i'm using this, which is sloppy and slow:

if ($chr = fgetc($file)) != "") { }

which of course reads in one byte at a time till the end of line. is there
a better way?

Replace A Line In A Text File
I would like to replace a single line in a text file. The initially i only had to have a single line so I could just use fputs to rewrite the entire file.  Now I have three lines and want to selectively replace one of them..

Detect Line From Text File
I have a text file that list out several activation code
aaaaa1
aaaaa2
aaaaa3
aaaaa4
aaaaa5
Then I will create a form which user will enter the line number of the text file to get the activation code. Eg user enter '2', so the system will pop out 'aaaaa2'.

The Problem is I really stuck on how to get php call the line number in the text file.

How Do I Read A File Line By Line Backwards?
I need to read a txt file backwords line by line.

Read The Whole Txt File Content Instead Of Line By Line
how to read the whole txt file content instead of using "fgets" and read line by line.

Reading Strings In Text File (1 Per Line)
I am trying to take a text file containing a list of email addresses (1 per line), and insert each line as a new item in mySQL.

I know how to do most of the php and the mySQL, but I am not sure about the syntax of the line to return the string from "line x" of the tex tfile.

From the php doc. I have this code to read the full contents of my text file:

// get contents of a file into a string
$filename = "/usr/local/something.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);

My question, clearly is:
then how do I split $contents into an array so that $emails[150] could return the email number 150?

Deleting A Line Of Text From A Txt File From A Form
How can i delete a line of text from a .txt file using a form?

because i have a form right now that saves information from a form to a txt file for news, now i need to know how to make it so i can delete it from a form.

also, if you know how, how do you overwrite lines of text in txt files from a form?

Appending Text To A File, Starting A New Line.
Im appending text to a file. And I'd like each 'append' would start on a new line of a file. I tried 'n'. but it doesnt work. here is my code:

$fp = fopen("$filename", "a");
for ($i = 0; $i < count($arrText); $i++){
fputs($fp, $arrText[$i]);
} fclose($fp);

note: one of the array $arrText, eg. $arrText[3] = "line 3" . "n";
the next array $arrText[4] will NOT start on a new line, it will just joint the rrText[3].

Removing Line Breaks In Reading Text File
When I read in a text file seperated by | delimiter, all is well except in the case of data posted to that text file from a Textarea within a Form. When the user hits the Return key once, a new line is ultimately created in the final file read; when the user does a 2nd or subsequent CR, there are multiple blank lines on the file as it is echoed into a web page (in fact, 7 <BR>s in a row).

I've tried str_replace to no effect ...is it a PHP error or is it something logical I've missed. Lines 5a, 5b, 5c, 5d are the results of a Textarea submission - they aer all one field - that have been seperated by a carriage return in an html form posting to a text file: Code:

How To Echo A Single, Specific Line From A Text File
How do I echo a single, specific line from a text file?  Let's say, for example, the second line?

Read A File Line By Line
Is there a function to read a file line by line in PHP, i.e. the way you do in Perl like <file_handle> ..... Note that i don't want to use the file() function.. that read entire file into an array and is not suitable when you're reading large files.

Delete A Line In PHP
What i want to do is to open a file read the file Select a particular line from the file and delete that line and close the file. How to do it in PHP.

Search Array Instead Of File For Matches
what I want to do is search by putting the file an array so I can page through using an offset. I have it all working except for this snipplet that is below I have searched for something that will work with no cigar. I my file is in this format.

100012|billybob|billybob@aol.com|3/10/2003|000-555-1212|

I want to beable to do a partial match like with (preg_match("/" . $Searchstring . "/", $line))and (preg_match("/^$Searchstring/i",$line)) If I just use the plain $Searchstring withouth the jazz I need a exact match I don't want that. PHP Code:

CLI ( Command-line ) PHP Displaying Source Code In-line When Programis Executed
I'm using PHP 4.4.2, and use PHP on both the command-line and the web.

I am running PHP on SuSE 10 Linux , in a VMware 5.5 workstation, using
Apache 2.0.55 , on my Dell laptop. Everything has been running flawlessly
without problems. Very amazing to use VMware, it has worked beautifully.

uname -a

Linux xxxxxxx 2.6.13-15.8-default #1 Tue Feb 7 11:07:24 UTC 2006 i686 i686 i386
GNU/Linux

php -v
PHP 4.4.2 (cli) (built: Feb 7 2006 20:13:29)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

php -m

[PHP Modules]
bcmath
bz2
calendar
ctype
curl
dbx
dio
domxml
exif
ftp
gd
gettext
gmp
iconv
mcrypt
mssql
mysql
ncurses
openssl
overload
pcre
posix
session
shmop
sockets
standard
sysvsem
sysvshm
tokenizer
wddx
xml
yp
zlib

When I run the following program from the command line it prints out the
source code as well as the output from the program. I run other PHP programs
on the command-line and this doesn't happen. It's really peculiar. The
error output is below.

I copied this off of php.net, and have modified it only slightly to allow
command-line input. I was trying to find a CSV solution, which I ended up
finding elsewhere, but am curious why this program would error out the
way it does--have I misconfigured something? My php.ini is basically
unchanged, if at all. I think I might have set the path to mysql but that's
about it. When I run this program from the web it works without error.

<?php

if ( $argv[0] )
{
$file = $argv[0] ;
}
else{
$file = $_GET['file'] ;
print "<font face=arial>
" ;
}

$row = 1;

$handle = fopen("$file", "r");

while ( ( $data = fgetcsv($handle, 1000, ",") ) !== FALSE )
{

$num = count($data);

print "$num fields in line $row:<br>
";

$row++;

for ($c = 0; $c < $num; $c++)
{
print "$data[$c]<br>
" ;

Append Text At End Of Existing Text On Same Line
I'm working on a part of my web site that uploads images, then writes the name of the image to a text file. The upload part works fine. The part that writes the name of the image to the text file works fine.

What I'm trying to do is, after all the files have been uploaded and all of the image names have been written to the WriteTo.txt file, append a written description of the picture at the end of the picture name. In other words, after a user uploads their pictures to the server I want to direct them to a page where they can label each of the pictures using a form. PHP Code:

Reading Files Line By Line Backwards
I'm new to PHP, and I'm trying to write something for my website. I have coded a game together with a friend of mine, and as with all new games and programs, it's full of bugs.

What I would like to do, is write a page where everyone can post bugs (I have this part, not that hard). Then it should be read from the textfile they're all in, but backwards, so the newest bugs at the top of the list.

Also I would like to add an option for me to login using a username and password, to mark bugs as fixed, or delete certain bugs from the list. I have the user part covered too, but I don't know how to delete a single line in the textfile.

Line Break In Text Box To < Br >
I made a little form that allows me to post news. When submit is hit, it just writes to an html file. I want to know, if i make a line break in the text box when im entering news, how can i make that carry over to a < br > tag.

Line Return In Text Box
I have a text box where users type whatever. Anyways, I am using stripslashes() and posting the contents to mysql table as TEXT. Every where I have used br it echos the newline plus rn, which i assume is supposed to be with the slashes removed.

So I don't have to input br everytime I press enter, how can I catch when the enter key is pressed and output a new line, like the way this forum is doing it?

Selecting A Line Of Text
I'm trying to display a bunch of lines of text one line at a time, like a code printout, and allow the user to select one of the lines by clicking on it. Obviously, the displaying part isn't so hard. But i'm not sure how to select and highlight a whole line with a single click, or how to figure out which line has been selected afterwards. How does one normally do something like this?

String Line Parsing
I have form text I need to parse and process. When I use sscanf to get a line it doesn't always act the same.

$data = sscanf($IncommingDATA, "%s
");

My incomming data is like a file with tabs between items and return ending a line. The php functions don't seem to like this and often work backwards than I exspect? Someone got a way to grab a line parse it and grab the next line?
Note: Text from a form not a file!

All Text Email - How Do You Add Line-breaks?
Sending an all text email through php.

How do I put line breaks in the message, I was unable to google it

I know you can use "", but like, what else can I do, what is this stuff even called, language, so I can find out what the stuff does?

How To Capture The First Line Of A Text, Then The Rest?
How can I cath the first line of a .txt file or whatever in PHP, using include("")? I need it for this, like, I'm thinking of doing a site with reviews on it, and I want to have one .php file for all the reviews. So that I can open a review using review.php?review_id=photoshop6

Im planning to have each review as one txt file, that I write "by hand" with the first line as the title. (If anyone knows of a better, smarter and faster way to do this, let me know, i'm new with PHP. With MySQL or whatever.)

The script should first parse the first line of the .txt file and use that one for title, so i can use a different size of text for it, and then it should parse the rest of the file and use for body text.

Detecting Line Breaks In A Text Area?
This is really frustrating... I have a form in which there is a text area for people to post comments on the site. I'm hoping that people will take the time to fill out reasonable length responses which my spread over two or more paragraphs. My problem lies there; it might spread over two paragraphs...

I know I can use 'special character(s)' which the user can put in that I can then do a ereg_replace() to put the <p> or <br> tags in but I would prefer that they didn't have to... like for instance in this forum

I've tried to see whether there are any line breaks or something in the textarea when a carriage return has been made but I can't seem to pick anything up, has anybody managed to come up with a solution to this?

Stripping Of All Spaces And Line Returns From A String
When I get a string from a form feield being sbmitted from a web form, I need to strip out all non needed spaces, remove all carriage returns and line feeds, to essentially give me one long line of text for the string.

Does anyone know of the code that woudl help me acheive this?? I would be greatful if Anyone could help me with this little problem. I have heard that ereg_replace can use a lot of CPU so I would like a solution with speed considerations.

Remove Certain Line Write Certain Line
I read a file and check wether the name of the thing they are rating is in it. Now, I want to make it that if it is then copy the line already in the file (in array) remove that line from the file add some values together then put it back in and the same place it was in ofr maybe just the end.

How Do I Get The Line Current Line Number ?
For error handling, is there a way to determine what line number the code is on, or do i just reference that line when im calling the error handler ?

Actually the custom error handler is i suppose called by ph itself, so you arent suppose to call this directly, rather if you want to explicitly trigger an error, you should use trigger_error, correct ?

Is There A Php Command To Take A Specific Field From A Delimited Line Of Text?
I have a string which is multiple fields delimited by commas.

I would like a php command that says... take the 3rd field for
example.

ie. If MYVARIABLE has the data "hello,there,big,world".

I would like to be able to say, pickup the 3rd field "big" without
knowing the character positions or lengths.

Is There A Way To Take An Html String And Render It As A Web Browser Would, From The Command Line?
I'd like to write a PHP script to be used from the command line on a
Unix machine. I'd like for the script to put together a string, turn it
into a web page, print it, then return control the command line.

Obviously it is quite easy to make a string that is valid HTML and send
it to the printer. The tough part is making look like a web page. In
particular, image tags should print out as images, not image tags.

Displaying Japanese Characters In Subject Line Of Email Using Php String
$ResponderSubject = "&#12486;&#12489;";

I have the above line of code, which displays some Japanese characters. This particular string will be displayed in the Subject line of an email. The problem I'm having is that the actual ASCII characters are being displayed instead of the Japanese characters. Is there a way to fix this?

Line Feeds With MIME-Version: 1.0Content-Type:text/html; Charset=iso-8859-1
I added "MIME-Version: 1.0
Content-Type:text/html;
charset=iso-8859-1
" as one of the parameters in the mail( )
function to replace the "Nobody" with a real From: email. That worked.

But if I attempt to add any other terms, such as the sender's name and
location, the MIME parameter above seems to ignore the "
" line feeds
and put the these other terms ahead of the mail message.

Here's my code:

mail("$toaddress", "$subject", "$mailcontent", "MIME-Version:
1.0
Content-Type:text/html; charset=iso-8859-1
From: ".
$youremail
. "
" . $yourname . "
");

The value for $yourname appears ahead of the message [$mailcontent] on
the same line. What I would like it to do is display the message, go
to a new line, then display $yourname.

Getting The Last Line Of A File
I am writing a script which needs to read the last line of a file. I don't want to read the whole file and then seprate the last line as some of the files will get pretty big.

Is there a function (Something like tail from unix) to let me do that? I tried searching the php site but didn't get far.

A New Line In Txt File.
I wrote a php script which can generate a report line by line. I did it by fopen function and create a new line by " ". Code :

New Line In A Txt File
I'm doing a script which insert text in a .txt file. eg,

$fp = fopen(sth.txt,'w');
$string = "text n";
fwrite($fp, $string);

The problem is that the n dosen't work!, how can I insert each line in a new line?

How To Add To The First Line Of A Txt File?
I'm trying edit a script called shoutbox and it simply just adds a name and comment to a txt file on a new line and includes it into my webpage via php.

My problem is that it adds the new comment to the bottom... I need it to add that comment to the top in a accending order.

$lines = file($file_path);
 
 $handle = file_connect($file_path, 'a');
 
 if(@fwrite($handle, $message['name']." | ".$message['url']." | ".$message['message']."")) {

I've tried w+  but it erases the previous comments added to the data.txt file.
Does anyone know how I would add a line to the top of the data.txt file without first erasing it?

Replace The Last Line Of File
I have a text file and I want to replace the last, and only the last of the file with some other text I store in a variable.

Removing A Line Within A File?
I have an include file (links.php) with nothing but links, the filenames are dates ( i.e. YYYYMMDD_hhmmss.php), all the links point to files within the same directory (i.e. /data). I read all these files in from the directory with readdir() and then build a form in order to remove certain files. I need to be able look into the include file, find the correct link, and remove it. I have been trying to use a foreach statement and preg_match() and printing back to the file lines that do not match and skipping the ones that do with absolutely no luck. It doesn't seem to match the link within the file.
Here is a snippet of code:

Rewrite File Line
I am trying to write a portion of the script that will replace a line with a new one in a text file. I figured fputs would do the trick, but I tried opening the file with fopen with the mode r, r+, and w+ and they don't seem to do the trick. r will only read, r+ is supposed to read and write, but instead it just reads (nothing new was inserted when I tried it), and w+ erases the file then inserts the new information.

The way the script works, it will search for the proper line using a while loop, once found, it will take the information and modify it slightly, then it's supposed to overwrite the new line over the old one, afterall, the pointer have not changed it's position since it found the correct line.

I don't want to replace an entire file because the script will do more work, just one frigging line in the same position.

Editing One Line Of A File.
MySQL ahs been giving me tremendous problems ever since I tried to switch to version 5.0.20. I thought that I might make my own SQL program, out of PHP code.

But if a user of it has a table with hundreds or more entrys, editing one line would take hours. If you can't get PHP to edit one line with a few functions, what about splitting a file into three parts and then merging them together after editing the middle part, like so: Code:

Line Replacement Within A File
is there an easy way to take a file, and within that file, replace a line? I am taking a source text file, and appending to the end of the destination text file, and then in the destination text file, replacing certain lines of text with something else.


Copyright © 2005-08 www.BigResource.com, All rights reserved