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


SuperbHosting.net have generously sponsored dedicated servers to ensure a reliable and scalable dedicated hosting solution for BigResource.com.





Delete Lines From File:


How to delete $n first lines from $str if i know how much lines shuld remain in the string?

$str = "aaaaaaaaa
bbbbbbbb
ccccccccc
ddddddddd
...
zzz";

Can it be done easly with preg_replace? (i.e without splitting it by '', removing elements from array and joining tham back.




View Complete Forum Thread with Replies

Related Forum Messages:
Delete Lines From A File
Can someone show how I open a file and delete everything in it except for the last 20 lines.

View Replies !
How To Delete N Lines From The Top Of A File?
I'm not sure how to do this in php - need to calculate and delete an unspecified
number of lines from the *top* of a file.

<?php
//append $visitor to the bottom of $visdata
$fp = fopen($visdata,"a");
fwrite($fp, "
".$visitor;)
//count the lines in $visdata
$lines = count($visdata);
if $lines > 10
{
//delete as many lines off the top as necessary
//to end up with 10 lines total in $visdata
}
fclose($fp);
?>

View Replies !
Delete First 9 Lines In Text File
how I can delete the first 9 lines in a text file?

View Replies !
Delete New Lines
I would like to delete the new lines, I mean the, so Itried this:
str_replace($row['facts_conten t'], &quot;
&quot;, &quot;&quot;) ;
But the new lines are still there, how can I do?

View Replies !
Open File, Put First 5 Lines Into A Variable, Close File...
1) I want to open a file on my server.
2) I want to read only the first 5 lines.
3) Put the first five lines into a variable.
4) Close the file.
5) Display the contents of the variable.

I know how to use the fopen() but I have big problems just trying to read the first 5 lines only? I tried many things. I downloaded some search scripts which displayed the <meta> tag only from html pages...

What is this guy doing?

Well I have a big site and content is added and changed everyday. I have a dynamic page that reads a directory for files and then puts a link (well it includes the file for reading on my php page). That works great! MY PROBLEM is that I want not only a link but a small description below it (basically the first five lines on the file)...

See so far:
Quote: if (isset ($content)) {
include ("files/$content.html");
echo "<p><A HREF="read.php"><b>Back to index</b></A>";
} else {
echo "<b>Opening directory, reading files, and listing them as a link</b><p>";
$handle = opendir('./files');
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo eregi_replace (".html", "", "<A HREF="read.php?content=$file">$file</a><p>");
}
}
closedir($handle);
}

View Replies !
File Lines
tell me how i can include the last 10 lines of a file on a page with php and how i can include line 4 to 20 with php and how i can include a random file.

View Replies !
Read First 10 Lines From A File
I have an ASCII file with each data record on a separateline. Each field is
delimited with a TAB (ASCII 9) character.

Now i want to read the first 10 lines and show them on my site.

This is what i already have reading the first line:

$fname = "http://www.somedomain.com/file.txt";

$fd = fopen($fname, "r");
if ($fd) {
if (!feof ($fd)) {
$buffer = fgets($fd, 4096);
$field = split(chr(9), trim($buffer));
$field0 = "$field[0]";
$field1 = "$field[1]";
}
fclose($fd);
}

echo ("$field0 - $field1");

How can i do this voor the first 10 lines?

View Replies !
How To Loop Through Lines In A File?
I have an array ($data_array) and I want to compare each line of a text file
($counter_file) to each element of the array (the array and the file are in
sync line-for-line). But the file is very big and I don't want to read the
entire file into an array.

In the interest of efficiency, I thought I'd open the file as read only and
loop through and compare each line to the array - since I have to open the
file later in append mode, I thought something like this would work. But
how do I advance the pointer to the next line in the file as the loop
increments?

$fp = fopen($counter_file, "r");
for (i=0; i < count($data_array); i++)
{
$recent_visit = fgets($fp); //<<== * ?
// * how to go to next line as i increments?
if ($recent_visit < TIME1h)
{
$dup_line = explode("|", $data_array[i])
$dup_ip = trim($dup_line[i]);
if ($visip == $dup_ip) //no dupes within one hour
{
exit;
}
}
else
{
break;
}
}
$fp = fopen($counter_file, "a");
[append stuff to counter_file here]

View Replies !
Edit Lines In Txt File
i am trying to write data that i have stored in a session back
to a member.txt, but i am unable to work out why it wont write. It
displays the data i have stored in the session ok, but it wont do the
next step! I have used a similiar script in another part of my web
page which works, but this one won't. From my understanding it has
something to do with if statement if($data[0] == $user_id), so i might
be trying to compare the previouis data to the current data stored in
the txt file properly....

View Replies !
Printing Only Two Lines From A File
I have a file (file01, i.e) like this:

foo
foo
foo
bar
foo
bar
foo

....

Where foo and bar repeats n times, but only foo and bar. What i want to
print is just:

foo
bar

I have tried this:

$myfile=file("file01");

$current_line = reset($myfile);
$first_line = $current_line;

print("First line is: $first_line");

while ($current_line=next($myfile))
{
if ($current_line != $first_line)
{
print("The other line is: $current_line");
break;
}
}

and it works as stand alone, but in the real application, i get:

Fatal error: Cannot break/continue 1 level in
/var/www/html/web/include/functionShow.inc on line 172

View Replies !
Using File() With Blank Lines
Let me admit right up front this is my first foray into php. I'm trying
to simply use a small data file to populate a page. I'm using the
file() function but I want my "data records" to be seperated by a double

My problem is I can't seem to compare a line to anything. My first
problem was determining the end of a data block, finding the double
newline. But in frustration, I just tried to compare a line to a known
value and I still can't compare strings correctly.

Here's a sample data file:

View Replies !
Creating A Pdf File On The Fly With 15 Lines Of Php
I have run across a php script that has about 15 lines of code. With a little re-work I have been able to send form data direct to a template I have created. The problem is, I also have images selected in the form, but the images don't show up... just the url of the image.

View Replies !
Sort Lines In A Txt File
I've formatted a long txt file with 300 lines, where each line consists of comma separated values with 60 fields for each line.

How could i sort all the lines with the second(counting from zero)element in each line as the "sort argument" Code:

View Replies !
Skip Lines In Txt-file
I have to process large files containing "logical" blocks of strings, each
block varying in number of lines.
....
Block number x
string1
string2
....
string n
Block number y
et cetera.

Blocks are read and processed one by one. My GetNextBlock ($index) function
returns an array with all lines in a block, and returns the line number of
the next block header.

What is the fastest way to skip those first <$index -1> lines the next read
? I cannot guarantee (unfortunately) the file is still open, so the file
pointer must be assumed at start of file again.

I had an intermittent solution in which I first split all the blocks into
separate files, but that caused a lot of other unwanted effects beyond the
scope of this posting.

View Replies !
Foreach And Lines In A File
I'm using the following to loop through every line in a file..

foreach ($lines as $line_num => $line){
//do stuff to each line here
}

..that works great. However I wish to start at line 6 of the file, so do exactly the same but skip the first 6 lines. There arent really any 'markers' that stay the same in each file i can just strip from. Cant seem to work this one out.

View Replies !
How Did He Make 10 Lines Read Only From File
How did he make 10 lines read only from file.

View Replies !
Counting Lines In A Flat File
I know that you can count the number of lines with PHP Code:

View Replies !
How To Read Only Some Lines From A Large File
I want to read only some lines from a large text file, say lines 27 through 45. So far, I used PHP Code:

View Replies !
Writing To Multiple Lines In File.
I was wanting to make a list of text field data enetered everytime someone filled out the form. I tried using fwrite() but everytime someone submits on the form it overides the previous entry.

I also tried fwrite($fh "$entry"); but it did the same thing. How can I do this?

View Replies !
Function Like File() But Will Read Lines X Through Y Only?
file() works great, until you start looking at file that are very large,
say 134MB.

Is there any function that will return an array for each line of a file,
but only for lines given as parameters?

A function like file("/path/to/file",100,230) which would return lines 100
through 230 as an array.

View Replies !
Reading Multi-lines In A File...
I'm trying to read multi-lines
from a flat file where a continuation is signalled by the line ending
ina backslash (ASCII 92)

Note 1: this is a windoze box
Note 2: My php.ini is set up with "auto_detect_line_endings = On"

Input file:

View Replies !
Read Only First 10 Lines Of A File Into An Array?
It's nice to be able to generate an html table from a PHP array. I know how to
do this, but the array in question is built from a file. The file in question
can be very long, and I only want the first 10 lines. So, I'd like to reduce
the overhead it takes to read the file into the array by limiting the number of
lines read in - rather than have the entire file read in and then limiting the
output to the html table.

$visits= file("visdata.txt");
//reverse the array so table is ordered by date decending
$visits = array_reverse ($visits);
//show only the last 10 visitors
$number_of_visits = count($visits);
if ($number_of_visits > 10)
{
$number_of_visits = 10;
}
echo
"<table border=&#390;' cellspacing=&#391;' width=̣“'>";
....
....
....
....

View Replies !
Deleting Lines From Flat File
I am trying to display all the news items in my flat file with 'delete' links (<a href="managedeletenews.php?action=delete&id=$key">Delete[/url]) that call the same page to strip out the line and rewrite the file.

However, when the delete link is clicked nothing happens (except the page's address changing from "managedeletenews.php" to the above.

Am I using the references to the $id or $key variables incorrectly?
Is it something to do with the undefined $action variable?
Or is it something else??
BTW, the embedded styles for the delete link - shown below - don't seem to be throwing up any errors. Code:

View Replies !
Reading Character From Lines From A File
I'm using file() to create an array of the lines from a .txt file.  I guess i have two questions.

1) how to I refer to each line individually after the array is created. 

    ie.  do action A to line 5.

2)  how would I search through that line in order to fine a specific character. 

ex.   1 Product Name: Macromedia Director MX 2004 Bible  so I want to loop through to ':' so I can cut of that part of the line and then display the rest of the line.

View Replies !
Parsing File And Printing Only Certain Lines
What's the best way to go about parsing through a file and only writing lines that come after x and stop when y is reached?

Example:
-- Begin X --
We are constantly
trying to improve
phpfreaks and these forums
-- End Y --

View Replies !
Reading A File, Selecting Certain Lines
I have a .txt file that looks like this:

image1.jpg, 15234
image2.jpg, 8752
logo.gif, 1402
blank.png, 120

But it's long, very long. The number after the image is it's size. What I need to be able to do:

Find all the lines that end in .jpg and output them to my screen (the filenames, not the actual images). I don't want to see the filesize and I don't want any gifs, pngs, bmps, etc.

Basically, I know how to put each line into an array, check to see if it ends in .jpg, and output if it does or not, but I don't know how to do this with multiple lines in a .txt file.

Basically I need to know the syntax for "read the first line" "check for .jpg" "check the next line."

I know how to check to see if it's jpg, just not how to go to the next line.

View Replies !
Flat File Display <i>x</i> Number Of Lines
I have a "site updates" script that I wrote that stores my site updates in a flat file in the form:

21234123|>|blah blah blah
21234123|>|blah blah blah
21234123|>|blah blah blah

where 21234123 is a unix timestamp.

what I want, is to be able to display the last x updates on my main page. I have a "updates.php" page which displays ALL updates... but I want to display just the say... most recent 3 or 5 updates at the bottom of my main page.

This is my function that displays -all- updates.

View Replies !
How To Strip Out Unnecessary Lines From Php Fopen File
I have a file that is stored on a computer on my network which i access using fopen for reading. The localpath to the file is http://www.example.com/output.txt

The contents of this file is updated once in a while and basically I need a php script that will open this file, read it and then remove certain things and then save a copy locally.

The contents of this file looks as follows:

Volume in drive C has no label.
Volume Serial Number is A85F-F7AD


Directory of C: est

10/01/2007 01:36 PM <DIR> .
10/01/2007 01:36 PM <DIR> ..
10/01/2007 01:32 PM <DIR> abc
10/01/2007 01:32 PM <DIR> 123
10/01/2007 01:32 PM <DIR> def
10/01/2007 01:32 PM <DIR> 456
0 File(s) 0 bytes
7 Dir(s) 68,797,751,296 bytes free

i want to get rid of the first 7 lines of the file and the last 3 lines. and then on each line i want to get rid of everything before the <DIR> including the <DIR> aswell.

View Replies !
How To Count Lines In A File That Meet Criteria?
Below are the contents file that has the IP address and time of visit of
visitors to a website.

68.122.69.241|1089822686

68.122.69.241|1089823630

68.122.69.241|1089823638

68.122.69.241|1089828547

The second column is the output of time()

I need a script to count each line in this file where the time stamp is
greater than time() - 3600*24. This will give number of visitors in the
last 24 hours. I also need number of visitors in the last 30 days, and
year. Should I use an array?

View Replies !
Function To Count Number Of Lines In File
Is There A Function To Count Number Of Lines In File?

View Replies !
Read A Text File Where Contents Are Going Over 2 Lines
I have this code to read the contents of a text file: PHP Code:

View Replies !
File Download Three Blank Lines At Start
I got this info from

and it works ok with saving the file on the server
but when I use the
&#392; - To create the file for the user to download'
the file always starts with three blank lines and
then the data, when opened with spreadsheet, text
editor, or pager.

I have tried both ways and the only real change is
the addition of the 'header(...)' lines. Any ideas
how to stop the header(...) lines from spitting
out newlines?

The second header line is one line.

<?php
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment;
filename="filename.csv"');
header('Pragma: no-cache');
header('Expires: 0');

snip..

echo $data;
?>

View Replies !
Use Eval() To Interpret Code Lines From A File?
I have a file called 'news.php' from which I want to pull lines of text, inside which are some variables that are pre-assigned. Code:

View Replies !
Using PHP, To Add/subtract Lines Of Text To A Flat File (xml)?
Is it possible, using PHP, to add/subtract lines of text to a flat file (xml)?

I understand I can use PHP "file write" to create a file, and "append" to add to it - but is there a way to then remove a certain line from it?

I understand this is sort of a content-management issue, but basically I want to give users the ability to manage (add/remove) songs in there XML playlist.

View Replies !
Multiple Random Lines From Text File
I have a text file with HTML fragments, one on each line. I want to display 4 of these lines, in a random order, with no repetition. Code:

View Replies !
Extract Lines From A Large Text File
I need to get Line 120 to Line 140 of a hypothetical 300MB text file,

$lines = file('textfile.txt');

That will effectively load the whole file and parse them as lines array for me to.

extract line 120 -140 easily.

Now that is of course a really inefficient way. If you are unlucky, you might get banned by your webhost for being a nuisance. Is there a way to move the file pointer to line 120 and read 20 lines from there instead of the inefficient way? Can we use FSEEK.

View Replies !
Download File And Delete File From Server With One Button Click
I want the user to be able to download a CSV file and delete the file from the server all with the click of a button.  Currently, in firefox, it will just display the CSV and not delete it. In IE, it will download the CSV and not delete it. Code:

View Replies !
Deleting Lines From Top Of A Large Open Text File
I am splitting up one huge text file in several smaller files to prepare them for database processing. So far I tested it with files up to 100MB and everything works fine.

Now here's my question: To optimize my script I would like to incrementally remove all lines form the original file which have already been copied to the smaller file.
This would speed up reading the original file in each loop.

Unfortunatelly I couldn't find a way to remove/delete lines from an open file. Of course I could read the whole file, delete the lines and then write it back. But for files bigger than 1 GB this doesn't seem to be a good idea....

View Replies !
PHP Flat File Database - How To Load And Edit Lines?
I am using a text file as a database, each field delimited by ||
I want to be able to print all lines to a page, then by selecting one
(with, say, a radio button) it will load into the form at the bottom of
the page, where I can edit the fields, and save back to the same line in
the text file.

I dont know how to use primary keys or anything with a text file
database,

View Replies !
Writing Multiple Lines Of Info To A Text File
when i uploaded my first .php file to my server it just displayed all the code and did not work. so i searched google for my problem and a guy said to save my file as a .php3 this worked but i am wondering if there is any other way to fix it. and if someone could tell me the difference between a .php and a .php3 file. Code:

View Replies !
Php Script To Filter A Text File And Extract Lines Starting With Keyword?
For a class, students are going to run an experiment on line. Each time
a subject runs, his/her data is appended to one giant text file. Their
own data set will be just one line starting with the keyword they gave
as identification.

The faculty does not want the students to be able to download and see
the giant data file. He wants the students to only download and see the
data that starts with their own identification tag.

in unix, filtering a file to keep only the line starting with code MCB
would look something like
tail -f your_file_name | grep MCB
from what I read.

Given the concerns the faculty has for protecting the database, what do
I need to look into to write a php script that would access the data
file, but only show a web page with the data corresponding to the
students identification code?

View Replies !
Blank Lines Between Lines Of Text
how do i put a blank line between lines of text on my webpage using php

eg within my while loop

printf("<a href="%s?id=%s">%s %s %s %s </a><br>
",$PHP_SELF,$myrow["id"], $myrow["first"], $myrow["last"], $myrow["email"],$myrow["details"]);

printf("");//this is what i thought might workdoes not work.

View Replies !
What Is The Best Way To Delete File ...?
I have script that allows a user to upload a txt file for processing, filtering, to save bandwidth,instead of user reuploading the file everytime he/she does a new process on the same file, i save the file to the server, and process from that file. What I want to be able to do is, when the user uploads a new file, or leaves the site, this temp file is automatically deleted (saves disk space).

View Replies !
Delete File();
I was wondering if php4 has a built in function that could delete a file. In my case I need my php to delete a picture. (jpg, gif) Does php4 have the ability to delete an exsiting file? Something like this?? delete_image("../images/somePic.jpg");

View Replies !
Delete A File
How do i delete a file from my server with php?

View Replies !
Delete File
How do I delete a file with PHP?

View Replies !
Delete File With Php
Im a little new in php, and just made a site where u can upload pictures, but how can i make it possible to delete pictures as well.

View Replies !
File Delete
I need to delete a compressed backup file after it has been sucessfully uploaded to a ftp server, the upload works fine using CRON and a php page. I have tried to delete the file using:

<?
$oldfile = "/home/#*$!/backup-10.8.2005_07-00-01_yyy.tar.gz";
unlink($oldfile);
?>

However I get a permission denied, what's wrong? Also, I don't know the exact file name, so what I really want to do is wildcard the file name like this:

<?
$oldfile = "/home/xxx/backup-*_yyy.tar.gz";
unlink($oldfile);
?>

Is this possible?

View Replies !
Using Php To Delete A File In A Directory- Help
I can use php to execute the cp command to copy an image file into a directory.

Now I want to delete images in that directory if they are not named "picturelink.gif" when an entry with a corresponding id is deleted.

Below is the code I am using which doesn't seem to be working when it comes to deleteing the file. The directory has the correct permissions for read/write access.

$query = "DELETE FROM $userstable WHERE id ='$id' ";

if($picture != "picturelink.gif")
{
exec("rm $picture /virtualhosts/capefearantiques.com/www/pictures/$picture");
}

View Replies !
How Do You Delete A File From A Listing Please?
I have the code:

<?php
if ($dir = @opendir("../project/proj_images")) {
while (($file = readdir($dir)) !== false)
if($file != ".." && $file != ".")
echo "<tr><td align="left" class="bodyblue2">$file</td></tr>";
closedir($dir);
}
?>

which displays a list of files in a directory.

What I want, is to be able to click on a file in the list and delete it.

I know you use unlink but how do you tie it in with the above code?

View Replies !
Can I Delete A File From A PHP Script?
Is it possible to delete a file(a jpg in fact) from a PHP script. I would like to ad this functionality to an existing script that deletes a record from a MySQL db. Only the image name is stored in the db.

View Replies !
Trying To Delete A File Using Unlink
i am trying to delete a file using the unlink function but it just is not working. i just get the Could not delete message. Is there anyway to find out exactly what is wrong? I've tried the exact path which is C:/domain/wwwroot/temp/0000000.txt but it just does not delete. Also i dont think it can be related to permissions because the 0000000.txt was created automatically by a script. The code i used is as follows:

<?
$delete = unlink("C:/domain/wwwroot/temp/0000000.txt");

if($delete)
{
echo "Succesfully Deleted";
}
else
{
echo "Could not Delete";
}
?>

View Replies !

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