Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shaddark's avatar

PHP - Find all anagram between 2 strings

Hello i need to write a class with an anagram function inside.

Verify that the anagram of a string is inside another string. Write a function:

public function anagramm(string $a, string $b): boolean {}

The function has to check that the string a is any anagram of b. The function must return true if it's an anagram, false if it's not. Assume that it's also case sensitive.

Example

Strings: A = "abc" and B = "abcdebacb" , the function will return true, because you can find the anagrams 'abc', 'bac', 'acb' in string B.

Thanks for help!

0 likes
8 replies
Sinnbeck's avatar

Those two string are not the same length so how would they be anagrams? Do you just want to find out if the letters abc are anywhere in the other string?

shaddark's avatar

@Sinnbeck Yes the string 'abcdebacb' has the anagram of 'abc'

'abcdebacb' has : 'abc' , 'bac' and 'acb' -> it's an anagram of 'abc'

Please or to participate in this conversation.