Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,903 questions

51,834 answers

573 users

How to check if two strings have the same words in different order with PHP

1 Answer

0 votes
function two_strings_have_same_words_in_different_order($str1, $str2) {
    $wordsOfStr2 = explode(" ", $str2);
  
    foreach (explode(" ", $str1) as $str1word) {
        if (!in_array($str1word, $wordsOfStr2)) {
            return false;
        }
    }
  
    return true;
}
  
$str1 = "java c# c c++ python php";
$str2 = "python c++ php java c# c";

if (two_strings_have_same_words_in_different_order($str1, $str2)) {
    echo "yes";
} else {
    echo "no";
}


  
/*
run:
   
yes
    
*/

 



answered May 9, 2024 by avibootz
...