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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    load text from a text file.. parsing??

    Hello all.. I was wondering if there was a way to load different paragraphs say 5 or 10 to different pages using a text document. I am pretty new to php and have read alot, but seem to be missing something. I can get the whole doc to load of course, but cant seem to get it to stop where I want. I am new and have been reading, but I missing something.. I am real new at this and I think I have read so much I am at a total loss. Is there a simpler way to do this?

    thanks in advance!

    here is the code I got from the net since I am new at this:

    the code

    PHP Code:
    $textfile"../doc/info.txt"
    $handle fopen($textfile"r"); 
    $contents fread($handlefilesize($textfile)); 
    fclose($handle); 

    $content explode("&noah="$contents); 
    print(
    $content[1]); 
    example data...

    just plain text document with multiple paragraphs

    for example:

    case above:

    &noah= blah blah approx 400 characters.

    next paragraph

    &sarah= blah blah approx 300 characters.

    and about 10 paragraphs below

    example output:

    full paragraph displayed on data specific web page only pertaining to that web page

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    IF you had an unique identifiable delimiter between the paragraphs you could make the file into an array wherein each paragraph would be an element in the array.

    For example, if you actually had the ampersand at the start of each paragraph AND did NOT use it anywhere else in the text file, then that could be your delimiter.

    the text file
    &noah was not really a boat builder, but no one was hring zoo keepers at that time. bla bla bla (or is that baaaa baaa baaa?).

    &sara on the other hand, had a bag full of baaa baaa baaa.
    the php

    PHP Code:
    <?PHP 
    $handle 
    fopen($textfile"r");  
    $contents fread($handlefilesize($textfile));  
    fclose($handle);  

    $array01 explode("&"$contents);  

    echo 
    $array01[0];
    ?>
    a couple of points to bear in mind (1) array elements begin with 0 (zero); (2) in our example since the first ampersand has nothing in front of it, the first array element will be empty. where as the 3nd element will contain the first actual paragraph; (3) note the use of brackets [ ] as opposed to braces {} or parens ().

    Hope this isn't too confusing

    Lite...

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok maybe I am not getting the text file understood correctly? no & infront of the first paragraph and the rest have " &name " with no " = " ? then call the code like you listed? and yes I am confused lol .. thank you so much for your help though.. if you have any more input I am always WILLING.. but I will not be able to play with it more till later..

    THANKS!!!
    Last edited by jeffhirsc; 05-05-2006 at 02:10 AM.

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Threw together a working example....

    http://www.nstoia.com/veni.php

    The script...

    PHP Code:
    <?PHP
    $filename 
    "veni.txt";

    #####################
    # open the file for reading

    $handle fopen($filename"r");

    ############################
    # read the entire file into a variable

    $contents fread($handlefilesize($filename));

    #####################
    #    close the file
    fclose($handle);

    #################################
    # convert the file contents into an array. 
    # In this example the elements of the 
    # array separated by the ampersand

    $array01 explode("&",$contents);

    ##############################
    #     print the array elements
    #########################################
    #    first count the number of elements in the array

    $count count($array01);

    #####################################
    # setup the table

    echo "<table width = 500 border=1 cellpadding=4>";

    ######################################
    #    loop through the array elements.
    #    putting each in its own row/cell

    $i=0;
    for (
    $i=0;$i<$count;$i++) {
        echo 
    "<tr><td>";
        echo 
    $array01[$i];
        echo 
    "</td></tr>";
    }

    #####################################
    #    close the table

    echo "</table>";

    ?>
    Lite...

  • #5
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    lite..

    THANK YOU so much for your help.. I am trying to undertsand this whole mess and I think you have been the biggest help! I have been reading all kinds of stuff for the past 3 days. preg_split , explode ect ect.

    I appreciate all your help here..

    I want to ask you more if it is ok.. what if I wanted to take each one of those tables and put them on a different page? .. I am going to play with it, but if you can reply that would be great! If I had some way to help you I would be glad too.. but you seem a little more experienced lol

  • #6
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    My pleasure.

    If you know or can determine which paragraph you want it is simple enough to display that particular one on its own page.

    the elements of the array are numbered starting with zero. so if you simply wanted to display only paragraph 7
    PHP Code:
    echo $array01[6]; 
    (remember the array number is 1 less than the total number of elements. ie paragraph 1 is array element 0 --- $array01[0]; paragraph 2 is array element 1 --- $array01[1]; paragraph 3 is array element 2 --- $array01[2] etc etc etc)

    does that make sense to you?

    Lite...

    BTW it will help if you learn about sessions. it makes it easier to pass data/variable values from page to page.

  • #7
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hey lite...

    thanks again!! I dont know what sessions are lol.. but I am willing. I love PHP just dont really have any experience .. same with scc lol thats what I am using this on.. dont really know if I need it.. but it is something different.. BTW a friend of mine gave me another way to do this.. see what you think

    PHP Code:
    <?php
    $textfile
    "../doc/info.txt";
    $handle fopen($textfile"r");
    $contents fread($handlefilesize($textfile));
    fclose($handle);
    $output preg_match('/\[sarah\]([^\]]+)\[/'$contents$matches); 
    print  
    $matches[1
    ?>
    DATA:info.txt

    [sarah]Sarah’s Room <br>
    This former parlor has been professionally transformed into a delightful and convenient downstairs.. blah blah balh [ehome]

    [noah]Noah’s Room <br>
    This former parlor has been professionally transformed into a delightful and convenient downstairs.. blah blah balh [ehome]

    and so on..

    Output: on code inserted page..

    Sarah’s Room
    This former parlor has been professionally transformed into a delightful and convenient downstairs.. blah blah balh


  •  

    Posting Permissions

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