SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast TannerC's Avatar
    Join Date
    Mar 2005
    Location
    Orem, UT
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Searching for code, matching it, then echoing results.

    I don't even know where to begin with this on my own, so I'm looking to YOU for some guidance.

    Basically I have a txt file that has a few words in it, one word per line. I need a script that will look through that txt file, find the words and then see if there are any other matches for that word in the file. Once that has completed I want it to show what words it searched for and how many times those words were found.

    I tried using preg_match_all, but I can't figure out how to get the results I need. Can anyone help?
    Visit Creative Something, my creativity blog.

  2. #2
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How big are these files? It may be better to read the file into an associative array like
    $array[$word] = $count;

    Then you can easily get all of the words (array_keys) and the count $array[$word];
    MySQL v5.1.58
    PHP v5.3.6

  3. #3
    SitePoint Enthusiast TannerC's Avatar
    Join Date
    Mar 2005
    Location
    Orem, UT
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The files are not going to be very big at all, would you still recommend doing that?
    Visit Creative Something, my creativity blog.

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep. I like arrays a lot. My only concern was if the file gets too big, you're holding a lot in memory.
    MySQL v5.1.58
    PHP v5.3.6

  5. #5
    SitePoint Enthusiast TannerC's Avatar
    Join Date
    Mar 2005
    Location
    Orem, UT
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, pardon my noobness, but how do I go about this? I still can't figure out how to read the file, find a word, find the matches, then say how many matches were found.

    My main problem is that the words aren't predefined, the script has to find them on it's own.

    Is this even possible?
    Visit Creative Something, my creativity blog.

  6. #6
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's some modified code I use for something similar:
    PHP Code:
    $export "\path\to\file\customer.txt";
    if (!
    is_readable($export)) {
        echo(
    'Error connecting to the file ("' $export '").');
        exit();
    }
    $store_array = array();
    $handle = @fopen($export"r");
    while (!
    feof($handle)) {
        
    $buffer fgets($handle4096);

        
    $word ereg_replace("(\n|\r)"""$buffer);    //removes partial line breaks

        
    if (!isset($store_array[$word])) $store_array[$word] = 1;
        else 
    $store_array[$word]++;
    }
    fclose($handle); 
    I didn't test this code, but it's pretty similar to what I use.

    Now you can see if a word was found using (isset($store_array[$your_word])) and even see how many times it was found ($store_array[$your_word]).

    P.S. The code's a little dirty, but maybe another user will clean it up for you
    MySQL v5.1.58
    PHP v5.3.6

  7. #7
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function array_repeated$array )
    {
        if ( ! 
    is_array($array) )
        {
            return 
    false;
        }

        
    $duplicates = array();

        foreach ( 
    $array as $key => $val )
        {
            
    // Set the internal pointer of an array to its last element
            
    end$array );

            
    // Fetch a key from an associative array
            
    $k key$array );

            
    // Return the current element in an array 
            
    $v current$array );

            while ( 
    $k !== $key )
            {
                if ( 
    $v === $val )
                {
                    
    $duplicates[$key] = $v;
                }

                
    // Rewind the internal array pointer 
                
    $v prev$array );
                
    $k key$array );
            }
        }
        return 
    $duplicates;
    }


    //  Load  file  into  Array
    $list file'x.txt' );

    //  Convert  every  line  to  lowercase
    $list array_map"strtolower"$list );

    print_rarray_repeated$list ) );

    echo 
    countarray_repeated$list ) ); 
    my mobile portal
    ghiris.ro

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So you're just looking for values which were repeated?
    PHP Code:
    <?php
    function moreThanOne($val){
        return (
    $val 1) ? true false;
    }
    $file file_get_contents('yourfile.txt');
    $words explode("\n"$file);
    $repeats array_count_values($words);
    $repeats array_filter($repeats'moreThanOne');
    var_dump($repeats);
    Or, if you like it consise:
    PHP Code:
    <?php
    function moreThanOne($val){
        return (
    $val 1) ? true false;
    }
    $repeats array_filter(array_count_values(explode("\n"file_get_contents('yourfile.txt'))), 'moreThanOne');
    var_dump($repeats);
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •