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

  • Home
  • SEARCH
  • 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 7969623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:15:59+00:00 2026-06-04T07:15:59+00:00

I’m totally a newbie with PHP. Today I just got a problem that I

  • 0

I’m totally a newbie with PHP. Today I just got a problem that I can’t know how to solve, even after searching google and digging SOF. It’s the Anagram algorithm.

So basically, I understand the problem here : When user input a string, I split it and compare with my library (a given array), then I will have to join it by 2-3-…etc characters to compare again, it’s exactly where I’m stuck now, I don’t know how to join the elements of the array.

Here is the code that I’m implementing, and also a sample dictionary.

I have a self-made dictionary with these elements in the array $dict. And i have a form for users to input a string, the string inputted will be passed to the code below and declared as $anagram. I have to split the string inputted to compare with my dictionary. But I don’t know how to join them like comparing 2 letters, 3 letters…etc…and so on, to the dictionary.

<?php

$dict = array(
'abde',
'des',
'klajsd',
'ksj',
'hat',
'good',
'book',
'puzzle',
'local',
'php',
'e');

$anagram = $_POST['anagram'];
//change to lowercase
$anagram = strtolower($anagram);

//split the string
$test = str_split($anagram);

//compare with $dict for the first split without joining
for ($i=0; $i<strlen($anagram); $i++) {
    if ($test[$i]==$dict[$i]) {
        echo $test[$i]."<br />";
    }
}

//problem: how to join elements of the array in the loops
//like user inputs "hellodes"
//after echo "e", how to join the elements like: h-e,h-l,h-l,h-o,h-d,h-e,h-s
//and then h-e-l,h-e-l,h-e-o...etc...
?>

I hope to get the algorith as simple as possible because I’m totally a newbie. And I’m sorry because my english is not so good.
Best regards,
Khiem Nguyen.

  • 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-04T07:15:59+00:00Added an answer on June 4, 2026 at 7:15 am

    (I’m adding this as a separate answer, as it’s a different way of dealing with the issue than I mentioned in my first issue)

    This is a more complex way of working out which words in the dictionary are part of the word that you’re looking for; I’ll leave it up to the reader to work out how it works.

    It’s using factorisation to work out whether a word is an anagram of another. What it will do is assign each letter a unique, prime value; you can calculate the value of the letters in a given word by multiplying all the values together. CAT, for example, is 37 * 5 * 3, or 510. If your target word factors to the same number, you can be sure that the one is an anagram of the other.

    I’ve ordered the prime numbers by how common they are in UK English, to keep the factors generated smaller.

    <?php
    
    function factorise($word)
    {
        // Take a number, split it into individual letters, and multiply those values together
        // So long as both words use the same value, you can amend the ordering of the factors 
        // as you like
    
        $factors = array("e" => 2, "t" => 3, "a" => 5, "o" => 7, "i" => 11,
            "n" => 13, "s" => 17, "h" => 19, "r" => 23, "d" => 29,
            "l" => 31, "c" => 37, "u" => 41, "m" => 43, "w" => 47,
            "f" => 53, "g" => 59, "y" => 61, "p" => 67, "b" => 71,
            "v" => 73, "k" => 79, "j" => 83, "x" => 89, "q" => 97,
            "z" => 101);
    
        $total = 1;
    
        $letters = str_split($word);
    
        foreach ($letters as $thisLetter) {
            if (isset($factors[$thisLetter])) {
                // This will skip any non-alphanumeric characters.
                $total *= $factors[$thisLetter];
            }
        }
    
        return $total;
    }
    
    $searchWord = "hasted";
    
    $dict = array("abde", "des", "klajsd", "ksj", "hat", "hats");
    
    $searchWordFactor = factorise($searchWord);
    
    foreach ($dict as $thisWord) {
        // Factorise each word that we're looking for
        // If the word we've just factored is an exact divisor of the target word, then all the 
        // letters in that word are also present in the target word
        // If you want to do an exact anagram, then check that the two totals are equal
    
        $dictWordFactor = factorise($thisWord);
    
        if (($searchWordFactor % $dictWordFactor) == 0) {
            print ($thisWord . " is an anagram of " . $searchWord . "<br/>");
        }
    }
    

    For what it’s worth, I think this is a much more elegant solution – you can speed it up by pre-calculating the values in your dictionary. If you go through and work out the factors for every word in your dictionary, you can do the searching direct in the database:

    SELECT word FROM dictionary WHERE wordFactor='$factorOfThisWord'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.