SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing a text file

    Hi,

    I am trying to access a text file which contains a list of postcodes, each on a separate line. I want to go through this file, checking to see if the postcode entered is valid. I tried using it, but realised that it wasnt searching the file correctly. I have put in the line echo "$numLines"; to see how many lines it says are in the text file, it says there is only 1, but I know there are more. Am I trying to access the file incorrectly? I have put the list.txt file in the same directory as the php file.

    Cheers,

    Paul

    PHP Code:
    $filename "/list.txt"// File which holds all data
    $inputString $postcode;
    $arrFp file$filename ); // Open the data file as an array
    $numLines count$arrFp ); // Count the elements in the array

    $numWords count$arrWords ); // Count the words in the string

    for($j=0$j<$numLines$j++) {
       echo 
    "$numLines";
       
    $outputstring "not found";
       if(
    strstr($inputStringtrim$arrFp[$j] ))) {
          
    $errorlist errorlist('Please fill in the "Postcode" field with a valid postcode'$errorlist); 
          
    $errorform 1
          
    $postcodestyle ' ; color:red';
          
    $outputstring "found it";
       }
       echo 
    $outputString// Echo the string replacing restricted words

    Last edited by pmorrison; Mar 9, 2006 at 04:50.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Hi and welcome to the forums!

    OK, heres how I would do it...
    PHP Code:
    $fileit file('list.txt');
    $pattern $postcode;
    $i 0;
    $pattern_match = array();
        foreach(
    $fileit as $line) {
            if(
    stristr($line$pattern)) {
                
    $i++;
                
    $pattern_match[] = $line;
            }
        }

    $count_parts count($pattern_match);
    if(
    $count_parts == 0) {
    // nothing found run your error part
    } else {
    // found something - 


    Hope this gives you an idea of the direction I am aiming for!

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Spike,

    I have tried it your way but when I click my submit button the page loads up with just the header and nothing else. It seems to be having problems opening the file, but it is definetly there and I have definetly got the name right!

    Paul

  4. #4
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have been looking into using text files with PHP and I am wondering if I need some kind of delimiter to show PHP that it is a new line. Currently each postcode is on a separate line, but just separated using a usual return, no \n or any other delimiter. Could this be a problem?

    Paul

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    possibly, can you post a snippet of the postcode text file so I can run a test on it?
    just 20-25 lines will do.

    Cheers

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go:

    AB101AF
    AB101AG
    AB101AH
    AB101AJ
    AB101AL
    AB101AN
    AB101AP
    AB101AQ
    AB101AR
    AB101AS
    AB101AU
    AB101AW
    AB101AX
    AB101AY
    AB101BA
    AB101BB
    AB101BD
    AB101BE
    AB101BF
    AB101BH
    AB101BS
    AB101BU

    Cheers

    Paul

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    OK, the problem is not with the file or the code....

    I ran this exact code
    PHP Code:
    <?php
    $fileit 
    file('postcodes.txt'); 
    $pattern 'AB101AQ'
    $i 0
    $pattern_match = array(); 
        foreach(
    $fileit as $line) { 
            if(
    stristr($line$pattern)) { 
                
    $i++; 
                
    $pattern_match[] = $line
            } 
        } 

    $count_parts count($pattern_match); 
    if(
    $count_parts == 0) { 
    // nothing found run your error part 
    echo 'Nowt here but a chicken....';
    } else { 
    // found something - 
    echo 'oooh yeah baby, I found you...';
    }
    ?>
    with the postcodes you gave me and it returned the success message 'oooh yeah baby, I found you...'

    Is this code doing what you want it to or have I missed the mark?

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, youve got it right, Ive tried the same code with that set of postcodes and it works, looks like the file that im trying to open is too big. Any idea what the maximum file size is?

    Paul

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Not sure really but I have a few text files that are 3-5MB each and run through them in a directory and it runs fine.

    What size is you file?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  10. #10
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its getting near to 10, but what I might do is split the file in half, creating 2 text files, then go through each separately!

    Its a bit of a botch, but itll get the job done!!

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    botches sometimes work better than coding!
    Good luck with it Paul, come back to me if you need more help

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help mate, Ill let you know when I get it working!

    Paul

  13. #13
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems that my web server will only let me open one file of about 800kb and anything more than that and it just returns a blank page. Therefore I cant just break the file up into many smaller parts. Is there any way of getting round this? I have tried using fclose, but Im not actually opening the file, apart from as an array, so I wasnt surprised when it didnt work. Is there any other way round this?

    Paul

  14. #14
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Evening Paul,
    You could breal the file into smaller parts offline and upload them into a directory, run through the directory and analyse each file for the postcode. Shouldn't add too much onto your processing time.
    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  15. #15
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using file() your reading the entire file into an array at once, using lots of memory.

    id be curious to see what this returns:
    PHP Code:
    echo get_cfg_var('memory_limit'); 

    have you tried just reading the file 1 line at a time?
    its very memory friendly, but slower.

    try using
    PHP Code:
    <?php
    $fp 
    fopen($datafile'r');

    while (!
    feof($fp)) {
        
    $line fgets($fp);
        if (
    match...) {}
    }




    ?>

  16. #16
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The result of echo get_cfg_var('memory_limit'); was 8M.

    I have now broken the files down and have created lots of if statements to ensure only 1 file is ever opened. However, I have 1 more problem to solve!

    When I search for the postcode it seems to be finding substrings of them as well as the actual postcode, for example, if I search for the (existing) postcode CT11SJ it returns 1, but if I search for CT11S (which doesnt exist) it finds 19 matches. I thought about using something like
    PHP Code:
    if($count_parts == 1
    to check for exact matches, but the result will be greater than 1 if the postcode I searched for happens to be a substring of another postcode.

    I tried
    PHP Code:
    if(strlen($postcode) == strlen($line)) 
    as another filter but it didnt seem to make any difference.

    Any help would be much appreciated!

    Paul

  17. #17
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Paul,
    Here is the full code from the script I wrote to search text files.
    It's quite straighforward to follow and simply recurse through the directory reading each textfile.
    PHP Code:
    <?php 
    /* 
    Processing time start
    */
     
    $m_time explode(" ",microtime()); 
     
    $m_time $m_time[0] + $m_time[1]; 
     
    $starttime $m_time;

    /*
    Get files from directory
    */
    $dir "text_files";

    // Open a known directory, and proceed to read its contents
    $files = array();
    if (
    is_dir($dir)) {
        if (
    $dh opendir($dir)) {
            while ((
    $file readdir($dh)) !== false) {
             if (
    $file != "." && $file != "..") {
                 
    $files[] = $file;
                }
            }
        
    closedir($dh);
        } 
    }
    /*
    file arrays...
    */
    if((isset($_GET['pattern'])) && ($_GET['pattern'] != '')) {
    $pattern $_GET['pattern'];
    $pattern_match = array();
    $i=0;
    foreach(
    $files as $read) {
    $file file($dir.'/'$read);
        foreach(
    $file as $line) {
            if(
    stristr($line$pattern)) {
                
    $i++;
                
    $pattern_match[] = $read;
            }
        }
    }
    if(
    $i == 0) {
        echo 
    'Sorry, your search for '$pattern ' returned no results';
        } else {
    echo 
    'Your search for <b>'$pattern '</b> returned '$i .' results<br />';
    /*
    strip out underscores and txt extention
    */
    $replace = array(".txt""_");
    $with = array(""" ");
    /*
    count the values and display the totals
    */
    $count array_count_values($pattern_match);    
    foreach(
    $count as $display => $data) {
        
    $display_data .= str_replace($replace$with$display) .' - (' $data ')<br />';
    }

        echo 
    $display_data;
    }
    }
     else {
        echo 
    'Please enter a search term';
        }
    ?>

    <?php echo $dir?>

    <form name="test" action="" method="get">
    <input type="text" name="pattern">
    <input type="submit" name="submit">
    </form>


    <?
     $round 
    3;// The number of decimal places to round the micro time to.
     
    $m_time explode(" ",microtime()); 
     
    $m_time $m_time[0] + $m_time[1]; 
     
    $endtime $m_time
     
    $totaltime = ($endtime $starttime); 
     echo 
    "Page loading took:"round($totaltime,$round) ." seconds"
    ?>
    HTH

    Mike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

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