PX : code

search_for_repeats by Greg Fowler
Download this code


<?php
/*
 *   Search a text file for repeated words.  Outputs (in p tags) the first discovered repeated word
 *   in the file if there are any, and "no words were repeated" if there aren't any.
 *
 *   Please email me if you find any bugs (gfowler07@yahoo.com), and see the comments for how to
 *   modify the code to suit your purposes.
 */
function search_for_repeats($filename)
{
    if(
file_exists($filename) && is_readable($filename))
    {
        
$handle fopen($filename'r');
        if(
$handle)
        {
            
$char "";
            
$word "";
            
$words = array();
            
$get_next_char true;
            while(
true)
            {
                if(
$get_next_char)
                    
$char fgetc($handle);
                if(
$char == false)
                    break; 
// reached EOF
                
$get_next_char true;
                if(
ctype_alnum($char) || $char == '-'// add or remove any characters you want to be included as part of words (note that ctype_alnum() also appears below)
                
{
                    
$word .= $char;
                }
                else
                {
                    
$word strtolower($word); // for storing each word in lowercase, so that case doesn't come into play in matching (you may want to modify this)
                    
$words[] = $word// store $word in list of found $words
                    
do
                    {
                        
$char fgetc($handle);
                    }
                    while(!
ctype_alnum($char) && $char != false);
                    
$get_next_char false;
                    
$word ""// reset $word to create next word
                
}
            }
            
$false_array array_fill(0count($words), false);
            
$found_words array_combine($words$false_array); // keys are words, values are true/false for whether they've already been encountered
            
foreach($words as $word)
            {
                if(
$found_words[$word])
                {
                    echo 
"<p>The word \"{$word}\" was repeated.</p>\n";
                    exit(); 
// you can comment this out and use boolean to check whether any words repeated before below echo if you want to output all repeated words
                
}
                else
                {
                    
$found_words[$word] = true;
                }
            }
            echo 
"<p>No words were repeated.</p>\n";
        }
    }
}
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.