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




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?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
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.

Reading A Single Line From A File
In asp I can read a text file line by line like this.

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("FileName.txt"), 1)
strLine1 = (f.ReadLine)
strLine2 = (f.ReadLine)
strLine3 = (f.ReadLine)
f.Close
Set f=Nothing
Set fs=Nothing
%>

I need to assign line 1 to one variable line 2 to a different variable and so on. Any help is greatly appreciated. Btw, I'm using php4.

How To Access A Single Line From A File
I want to select a single line from a file. How can I select a line from the file and save it in a variable.

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..

Is There A Way To Assign Multiple Variables A Single Value On A Single Line?
$f1, $f2, $f3 = &#391;'
and
list($f1, $f2, $f3) = 1;

neither work... second obviously cuz its looking for an array on the right
side, correct?

Right now i have ot build a for loop to assign the values

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 Single Email Adres From Text File
I have a textfile:

bla@domain.com
blabla@domain2.com
haha@domain.com
la@domain3.com
zaza@domain4.com

How can I delete a single email from that text file ? E.g. I want to delete blabla@domain2.com so that the textfile will be:

bla@domain.com
haha@domain.com
la@domain3.com
zaza@domain4.com

How can this be done ??

Search For A Specific Word Inside A Text File?
Basically what i'm trying to do is when a user inputs login/password information at a login page, I want PHP to search inside verify.txt and if it finds the login/password combination then allows the user to proceed. Is this possible? And if so, which functions would I use to get the job done?

Also, how can I save the login name so that it can be passed to/included in a url?

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.

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:

ECHO Or Concatenation With Single Quote, For Performance?
I am working on an HTML template which has a lot of html tags, with
PHP data shown in the middle of these tags -- you know, the usual.
Currently, I have HTML as is, and many many "echo $variable"
statements mixed in as PHP code.

My question: should I leave it like this, with ECHO statements
embedded within the tags,

Or

Should I concatenate the HTML within single quotes (single quotes are
supposed to be faster in PHP than double quotes) and the display data
as variables, and then ECHO that one concatenated string?

ECHO The Amount Of Rows In My Table According To A Specific Username
I am creating an infinite loop with this code. My goal is to just ECHO the amount of rows in my table according to a specific Username. How do I do this? my code:

<?php $query = "SELECT ID FROM products WHERE Username = '{$Username}'"; 
$result = mysql_query($query) or die(mysql_error());
$row = mysql_num_rows($result) or die(mysql_error());
$num = 0;
while ($num <= $row) {
echo $row;}?>

Echo: How To View/call Data From Separate File Into Echo
I would like to ask about echo "<--statement==>";

Ok my senario let say like this i got a file name as datax01.php & script.php

Let say on script.php i got a code to use echo. So how from echo i can recall datax01.php to view on that script? Code:

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"

How Would I Echo 3 Rows A Line Using PHP/SQL.
How would I go about echoing 3 rows at a time on different lines for example:

row1 row2 row3
row4 row5 row6
row8 row8 row9
etc...

Whats the best way to do this.

Echo Line By Line
this code gets all existing users:

function getAllUsers() {
$users = shell_exec('sudo /var/www/ftpadmin/./getUsers');
$output = shell_exec('cat users');
echo $output;
}


now echo $output gives something like:

user1
user2
user3
user4
user5
...

now I would like to echo this line by line like this:

<option>user1</option><option>user2</option><option>user3</option><option>user4</option><option>user5</option>

without line breaks!

Echo Multi-line Textbox Variable
I have a form that allows users to enter notes into a large textbox. After the form is submitted, the contents of the textbox are echoed out into a report. Below is an example of an entry:

These are my notes.
These notes doe not make any sense
but it gets the point across.

When I echo out this entry as a variable, the entry looks like this:

These are my notes. These notes doe not make any sense but it gets the point across.

I need to keep all of the notes just as they were entered on multiple lines. Does anyone have any ideas on how to fix this?

Hide Specific Text
I was wondering whteher anybody knows a code to hide a specific text on a site which u cannot control.

For Example, i am currently with a free hosting provider, and they put a simple text on my site saying "Free Web Hosting by something" Is there a way to hide this?.

It is not on my actual coding for the page therefore i cannot remove it. I thought there might be a way to block this text or something. It can be a code of any sort like php, html, asp or whatever. I just need to hide this.

Formatting Text For A Specific Width
I have a text file that will be written to with data from end users (text datatype).

The text document is formatted such that there is a column of text around 200px wide, set in the middle of the page.

Is there a way I can (or function that will) format a string (perhaps a paragraph or two) so that it will fit in this text document? Mind that the string is not a constant - it is entered by end users so I can't exactly hard code.
.

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:

Select Specific Snippet Of Text, Replace With A Database Result
I'm having some problems trying to do something; I just can't think of a way to do it. Basically, I want to search for a specific snippet of text in a body of text, then replace it with the requested database result with some thrown in HTML.

Users will be able to type the following (or something along these lines, depending on what works best): [image=12345]Insert caption here[/image]

Then I need it to search for the requested image in the appropriate table and return the image's url (for an example, let's say the table is called "images", and the columns required are "imageid" and "imageurl". Code:

Echo Text?
I have an image .gif, with text in it, but I want there to be no text, and echo the text in php.

Like say I have a .gif and it says Hello. I want to remove the Hello, so its just the bg. But the bg into a table, and echo out the text with correct font. The font is Bebas, so everyone might not have it. How can I do this?

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.

Echo Text Formatting
I'd like to format text that's returned as an echo from a form action ie. font colour alignment. Despite putting the css style on the php page I'm just getting default values. What's the secret please?

Echo To Text Box Only Returns First Word
I'm echoing values from a db to text boxes with php, but only the first words are returned. The db field is set to varchar(255). Can someone please tell me how to solve this small but annoying problem? By the way, I don't have any regular expressions or that kind of coding.

Echo Only Select Text From String
I want to echo a single sentence from about the middle of the string what is the easiest way to do this?

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.

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.

Updating A Single File
i want to update a single file some of the info i request is being display except for the picture and the second one is that it is not updating into the database i guess. i have to problem in one Code:

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?

Writing To A Specific Place In A File
I have a text file. I would like to write a line of data as the first
line and then another as the last line. What is the easiest way to do
this using php 4?

Fwrite At Specific Point In File
I'm doing a little feedback type feature on one of my sites, so a visitor can just type in their name and comment and it's posted to a page (feedback.html) which is displayed in an iframe.

This all works fine and dandy but when I write to the feedback.html page the new comments obviously go on the bottom, so that after a few feedback comments have come in you need to scroll down to read the new ones, so I would like it so that it posts the new comments at the start of the new file. This can't be literally the start though, as the feedback.html has three lines of headers, or 84 characters.

So what I need is a way to point the file pointer at the 85th character or the fourth line and perform the fwrite there. I've tried using fseek but haven't had any joy with that. Does anybody know what I'm talking about and is anyone able to help?

How To Parse A Doc File To Get Specific String
i want to parse a doc file like a resume or cv and just take out the value of name or a email id can nybdy pls tell me or give me the reexp to just extract the name or mail id fron the whole contents of doc file.

Retrieving Specific File Info...
Ive got a file which contains the following data:

<option value="13">African Nations Cup XXV</option>
<option value="16">asd</option>
<option value="7">Champions League</option>
<option value="1">English Premiership</option>

What Id like to do is get the maximum value, so 16. Ive used file_get_contents to bring the file into a string, but now Im stuck getting that max value out. Unless theres a function for this, I believe the direction I would need to take is collect all value numbers using some string manipulation function, throw them all into an array, and just get the max value of the array.

Is there an easier way?

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.

Single PHP File - *.php = Process, *.phps = Source.
Hoping for some direction here (I have searched, without avail, for a
solution already).

I was toying with trying to find a way whereby I could have a single
PHP file, and then specify whether to either see the processed output
or the source code by using the extensions php and phps (respectively).

So, with one file called "theFile.php" on the server, going to
"www.server.com/theFile.php" would show the end result of the php
actions, whereas going to "www.server.com/theFile.phps" would show the
actual content of the file itself.

I thought this could be done by modifying the htaccess file as follows:

AddType application/x-httpd-php-source phps

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+).phps$ $1.php

However this does not work:
- if a *.php file exists, then accessing *.php or *.phps returns the
end result of the php actions.
- if a *.phps file exists, then accessing *.php returns a 404 error,
and *.phps shows the actual file contents.

Looking for any direction, references, or assistance.


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