Results 1 to 8 of 8

Thread: return a single line from a text doc

  1. #1

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    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?

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: return a single line from a text doc

    Have a look at the file() function - it should help you

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    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?

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: return a single line from a text doc

    Quote 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
    My Blog.

    Ryan Jones.

  5. #5

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    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?

  6. #6

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    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?

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: return a single line from a text doc

    To get just the first line of a file use the fgets() function.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    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
  •  



Click Here to Expand Forum to Full Width