How to check whether two strings contain same characters in PHP

2 Answers

0 votes
function sort_string($s) {
    $arr = str_split($s);
    sort($arr);
    
    return implode('', $arr); 
}

function contain_same_characters($s1, $s2) {
    $s1 = sort_string($s1);
    $s2 = sort_string($s2);
            
    return $s1 == $s2;
}


$s1 = "php programming";
$s2 = "ingprogramm php";

if (contain_same_characters($s1, $s2)) {
    echo "yes";
}
else {
    echo "no";
}
    
       

/*
run:
            
yes
     
*/

 



answered Oct 28, 2019 by avibootz
0 votes
$s1 = "php programming";
$s2 = "ingprooooogrammmmmm phhhhhhhhpppppp";
 
// Remove all duplicate characters
$s1_tmp = count_chars($s1, 3);
$s2_tmp = count_chars($s2, 3);
 
if ($s1_tmp == $s2_tmp) {
    echo "yes";
}
else {
    echo "no";
}
     
        
 
/*
run:
             
yes
      
*/

 



answered Oct 28, 2019 by avibootz
...