|
-
Jun 2nd, 2005, 10:46 AM
#1
Thread Starter
Hyperactive Member
return a single line from a text doc
hi, using fread how do you get it to return a single line from a text doc. like with this kind of code
PHP Code:
<?php
$line = $_GET['line'];
$uid = $_GET['uid'];
$filename = "D:/inetpub/xproot/users/" . $uid . "fav.txt";
$handle = fopen($filename, "r");
$contents = //make $contents to be what is on line defined by $line;
fclose($handle);
echo $contents;
?>
i have googled this and all it had was some other things that had nothing to do with my question. this is something to do with a program i am making which uses msinet to get what is returned into a string or text box.
thanks, dandono
Last edited by dandono; Jun 2nd, 2005 at 11:02 AM.
If there is only one perfect person in the universe, does that make them imperfect?
-
Jun 2nd, 2005, 11:09 AM
#2
Re: return a single line from a text doc
Have a look at the file() function - it should help you 
Cheers,
RyanJ
-
Jun 2nd, 2005, 11:42 AM
#3
Thread Starter
Hyperactive Member
Re: return a single line from a text doc
ok i get what that function does but i dont get how to make it display only one line and that line being $line. how do i do this?
If there is only one perfect person in the universe, does that make them imperfect?
-
Jun 2nd, 2005, 12:00 PM
#4
Re: return a single line from a text doc
 Originally Posted by dandono
ok i get what that function does but i dont get how to make it display only one line and that line being $line. how do i do this?
Because the function returns the array, just pass the $line as the index to get the required line 
Cheers,
RyanJ
-
Jun 2nd, 2005, 02:44 PM
#5
Thread Starter
Hyperactive Member
Re: return a single line from a text doc
i think that this is not the best way to do this but it is the only way i can think. now somebody is going to come up with a code a houndred times better than mine in like five minuets.
this is my code. i will set it to open a local text file in a bit.
PHP Code:
<?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.php.net/');
$no = $_GET['line'];
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
if ($line_num == $no)
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
// Another example, let's get a web page into a string. See also file_get_contents().
$html = implode('', file('http://www.example.com/'));
?>
thanks for the help
If there is only one perfect person in the universe, does that make them imperfect?
-
Jun 2nd, 2005, 02:49 PM
#6
Thread Starter
Hyperactive Member
Re: return a single line from a text doc
how do you get that code to view a text doc on the server mechine like the path D:/blah.txt? is it just with fread?
If there is only one perfect person in the universe, does that make them imperfect?
-
Jun 2nd, 2005, 03:21 PM
#7
Re: return a single line from a text doc
To get just the first line of a file use the fgets() function.
-
Jun 2nd, 2005, 04:03 PM
#8
Thread Starter
Hyperactive Member
Re: return a single line from a text doc
sorry i fixed it a different way like this:
PHP Code:
<?php
$uid = $_GET['uid'];
$file = "D:/inetpub/xproot/xorite/browser/" . $uid . "fav.txt";
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file($file);
$no = $_GET['line'];
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
if ($line_num == $no)
echo htmlspecialchars($line) . "<br />\n";
}
// Another example, let's get a web page into a string. See also file_get_contents().
$html = implode('', file($file));
?>
If there is only one perfect person in the universe, does that make them imperfect?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|