Saturday, 28 February 2015

Finding comman characters between two strings aka String intersection

<?php

    /**
    * function to find comman alphabets in two string
    * @params $strOne and $strTwo , two strings to compare
    *
    * @return $comman string consisting a string of comman  characetrs
    */
    function commanAlphabets($strOne , $strTwo)
    {
   
        echo  " String to comapre  :  $strOne with $strTwo " ;
       
        //convert to arrays
        $charArrayOne = str_split($strOne);
        $charArrayTwo = str_split($strTwo);
       
        //implode to find intersections
        $comman =  array_unique (array_intersect($charArrayOne , $charArrayTwo));
       
        return $comman;
    }
   
   
    $result = commanAlphabets("AMITABH BACHCHAN" , "RAJNIKANT");
   
   
        echo "<br> Comman characters are as follows : <br> ";
        //print string of comman characters
        echo implode($result);
   
?>

No comments:

Post a Comment