How to check if strings are rotations of each other in PHP

1 Answer

0 votes
function rotations($str1, $str2) {
    if (strlen($str1) != strlen($str2)) {
        return false;
    }
      
    echo $str1 . $str1 . "\n";
 
    return strpos($str1 . $str1, $str2) !== false;
}
  
  
 
$str1 = "abcxd";
$str2 = "cxdab";
 
if (rotations($str1, $str2)) {
    echo "yes";
}
else {
    echo "no";
}
 
 
 
 
/*
run:
 
abcxdabcxd
yes
 
*/

 



answered Dec 23, 2021 by avibootz
edited Dec 23, 2021 by avibootz

Related questions

1 answer 176 views
1 answer 191 views
1 answer 98 views
1 answer 160 views
1 answer 165 views
...