Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts

    copy content of text file in variable

    How can I take the content of a text file and store it in a variable?
    What I ultimatly want to do is take that variable, extract what I want with regular expression and write that out..
    So if you could also point me to some good regular expression tutorial, that would be great.
    Shawn

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    try using fread

    http://www.php.net/fread

  • #3
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I don't understant fread, why do I have to give the filesize and stuff? Why can't I just read the whole file?
    Shawn

  • #4
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Here is the code I got.
    Code:
    <html>
    <body>
    
    <?php
     $handle=fopen('glossary.txt', 'r');
     $string=fread($handle,99999999);
     fclose($handle);
     echo($string);
    ?>
    
    </body>
    </html>
    How would I now use regular expression for get only te desired part of the text file?

    my text file contains this:
    -------------
    <table>
    <tr id="someID"><td>G</td><td>word</td></tr>
    <tr id="anotherID"><td>U</td><td>yay</td></tr>
    ...
    </table>
    -------------
    I want to make a function like this:
    function getWord($word){
    $handle=fopen("glossary.txt");
    $string=fread("$handle,99999999);
    fclose($hadle);
    $newString=regexp($string) //This should give me:
    "<tr id="$word"><td>*</td><td>*</td></tr>"
    echo($newString);
    }

    I might me mixing up with javascript syntax, but what I really want is to get starting tips and maybe links about PHP regular expression, I'll figure the rest out from there
    Shawn

  • #5
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What I think is the best and most understandable tutorial on Regex on the net.

    http://www.regular-expressions.info
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •