Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7888659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:53:19+00:00 2026-06-03T05:53:19+00:00

I came across this simple PHP Class on GitHub while searching for Bloom Filters,

  • 0

I came across this simple PHP Class on GitHub while searching for Bloom Filters, this was named as a “Bloom Filter” but I think it is more of a “Hash Table” either way I am curious, it is very simple to understand.

It reads in a file of words and creates a Hash Array Key for each word, you can then check if the word exist in the Hash Array.

I am curious though is there any benefit of using this versus just storing the actual word as the array key or value and then checking if that word exist in the array, in theory this would just be adding overhead and doing the same thing, please help me understand what I am missing?

<?php
class Dictionary {
    private $words;
    private $wordsHash;
    public $hashLength;

    public function __construct($filepath, $hashLength) {
        $this->words = file($filepath);
        $this->hashLength = $hashLength;
        foreach($this->words as $word){
            $this->wordsHash[$this->createHash($word)] = true;
        }
        echo 'words: ' . count($this->words) . '   hashes: ' . count($this->wordsHash) . "\n";
    }

    public function createHash($str){
        $hash = substr(md5(trim($str)), 0, $this->hashLength);
        return $hash;
    }

    public function checkDictionary($str){
        $hash = $this->createHash(trim($str));
        if(array_key_exists ($hash , $this->wordsHash)){
            return true;
        }
        return false;
    }

}
?>

dictionary.txt file has 10,000 words in it, I will just show a few for demo

der
die
und
in
den
von
zu
das
mit
sich
des
auf
für
ist

Example usage:

<?php
$dictionary = new Dictionary('dictionary.txt', 30);

if($dictionary->checkDictionary('den')){
    echo 'The Word den Exist in the Hash Table';
}else{
    echo 'The Word den DOES NOT Exist in the Hash Table';
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:53:21+00:00Added an answer on June 3, 2026 at 5:53 am

    The idea with this seems to be that searching for a key is much faster than searching for a specific value in an array. This is especially true for very large arrays. However, I would recommend a simpler approach to (as you already said) avoid overhead and collisions:

    $words = array_flip( file($filename) );
    
    // The actual values are now the keys!
    // So checking for a word works like this:
    if (isset($words['und'])) {
        // ...
    
    // Travling through the words works like this:
    foreach ($words as $word => $i) {
        // ...
    

    (PS: This code will not work as expected since every word will include the line break, so you will need to strip that first. But I hope you get the idea.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this class while reading a C# book and have some questions.
I came across this (really) simple program a while ago. It just outputs the
I've came across on this problem, I have a sever running apache and php.
I've recently been working with a simple Twitter API for PHP and came across
So I came across this bit of php code: <?php $long_url = http://example.com; $bit_ly
I was going through operator precedence section of php.net and came across this example
I was reading the book Extending and Embedding PHP and came across this line
I'm trying to make a simple page transition effect. I came across this one
i was searching about simple hashing with passwords in login form. i came across
I am learning JSF and came across this line: <h:messages layout=table></h:messages> in a sample

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.