How to check if two strings are anagrams in PHP?

PHP: How to check if two strings are anagrams in PHP?

This is a classic simple programming question. There are several ways to approach this problem. Here is one solution:

The comparison function:

  1. function is_anagram($a, $b)
  2. {
  3.  
  4.     if (strlen($a) != strlen($b)) {
  5.         // if not same size then they definitely aren't
  6.         return false;
  7.     }
  8.  
  9. // turn 'foobar' into ['f', 'o', 'o', 'b', 'a', 'r']
  10. $a_chars = str_split($a);
  11. $b_chars = str_split($b);
  12.  
  13. // sort them...
  14. sort($a_chars);
  15. sort($b_chars);
  16.  
  17. // check if they're exactly the same...
  18. return $a_chars === $b_chars;
  19.  
  20. // another way to do this that someone emailed me about, which is much more simple:
  21. // return (count_chars($a, 1) == count_chars($b, 1))
  22.  
  23.  
  24. }

Let's write some tests

  1. $anagrams = [
  2.  
  3.     // $a, $b, $is_valid_anagram
  4.  
  5.         ["asdf", "asdf", true],
  6.         ["asdf", "fdsa", true],
  7.         ["asdfasdf", "asdfasdf", true],
  8.         ["a", "a", true],
  9.         ["a", "b", false],
  10.         ["aaa", "aaa", true],
  11.         ["aaa", "aa", false],
  12.         ["aaaa", "abab", false],
  13.         ["qwerty", "asdfg", false]
  14.  
  15. ];

And now let's go through each one.

  1. foreach ($anagrams as $anagram) {
  2.  
  3.     [$a, $b, $is_valid_anagram] = $anagram;
  4.  
  5.     $result = is_anagram($a, $b);
  6.     $result_message = $result ? "Is an anagram" : "Is not an anagram";
  7.  
  8.     if ($result != $is_valid_anagram) {
  9.         throw new \Exception("The anagram function returned an incorrect result for $a and $b");  // this doesn't happen with our function! :)
  10.     }
  11.     var_dump("[$a] and [$b] $result_message");
  12.  
  13. }

"Our function said that [asdf] and [asdf] Is an anagram, which is correct"
"Our function said that [asdf] and [fdsa] Is an anagram, which is correct"
"Our function said that [asdfasdf] and [asdfasdf] Is an anagram, which is correct"
"Our function said that [a] and [a] Is an anagram, which is correct"
"Our function said that [a] and [b] Is not an anagram, which is correct"
"Our function said that [aaa] and [aaa] Is an anagram, which is correct"
"Our function said that [aaa] and [aa] Is not an anagram, which is correct"
"Our function said that [aaaa] and [abab] Is not an anagram, which is correct"
"Our function said that [qwerty] and [asdfg] Is not an anagram, which is correct"

BTW, not sure about how the [$a, $b, $is_valid_anagram] = $anagram; line works? See my blog post about array destructuring in PHP 7.

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to check if two strings are anagrams in PHP?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to destroy or remove a cookie in PHP

How to get the type of a variable in JS?

How does PHP compare objects?

What is Agile software development?

How to run an artisan command from a controller

How to review blog posts (written by other users) before they get published on your WordPress blog?

How to block an IP address in .htaccess

What is WordPress default Apache .htaccess rules?

How to prevent Eloquent from adding created_at or updated_at timestamps?

How to replace whitespace with a single space