Search code examples
phpregexpreg-matchmorse-code

Find potential Morse-code string matches using input with question mark as wildcard


I have an function that checks if string( parameter) matches values in an array and returns an array of possibilities key

function find_possible_match( $criteria ) {

  $possible_match = array();


      $possibilities = array(
"a"=>".-",
"b"=>"-...", 
"c"=>"-.-.", 
"d"=>"-..", 
"e"=>".", 
"f"=>"..-.", 
"g"=>"--.", 
"h"=>"....", 
"i"=>"..", 
"j"=>".---", 
"k"=>"-.-", 
"l"=>".-..", 
"m"=>"--", 
"n"=>"-.", 
"o"=>"---", 
"p"=>".--.", 
"q"=>"--.-", 
"r"=>".-.", 
"s"=>"...", 
"t"=>"-", 
"u"=>"..-", 
"v"=>"...-", 
"w"=>".--", 
"x"=>"-..-", 
"y"=>"-.--", 
"z"=>"--..", 
"0"=>"-----",
"1"=>".----", 
"2"=>"..---", 
"3"=>"...--", 
"4"=>"....-", 
"5"=>".....", 
"6"=>"-....", 
"7"=>"--...", 
"8"=>"---..", 
"9"=>"----.",
"."=>".-.-.-",
","=>"--..--",
"?"=>"..--..",
"/"=>"-..-.",
" "=>" ");


  foreach ( $possibilities as $key => $value ) {

         if( $value == $criteria ){
            array_push(  $possible_match , $key );
        }
  }

  return $possible_match;
} 

this is pretty standard is all criteria where string like

find_possible_match( ".-" );

will return [a]... etc

but the twist is that, what if the params has an unknown, example

find_possible_match("?");

should return [e, t], likewise

find_possible_match("?.")

should return ['i','n'] and

find_possible_match(".?")

should return ['i','a']

? in this case is the wildcard. How do i modify the above code to do just that. Thank you


Solution

  • You could use preg_match() you check if the $criteria match to $value. You could replace the $criteria according to regular expression requirements (escape dot, convert ? to [.-]):

    function find_possible_match( $criteria ) {
    
        $criteria = str_replace(['.','?'],['\.','[.-]'],$criteria);
        $regexp = '~^'.$criteria.'$~';
    
        $possibilities = array(
            "a"=>".-",
            "b"=>"-...",
            "c"=>"-.-.",
            "d"=>"-..",
            "e"=>".",
            "f"=>"..-.",
            "g"=>"--.",
            "h"=>"....",
            "i"=>"..",
            "j"=>".---",
            "k"=>"-.-",
            "l"=>".-..",
            "m"=>"--",
            "n"=>"-.",
            "o"=>"---",
            "p"=>".--.",
            "q"=>"--.-",
            "r"=>".-.",
            "s"=>"...",
            "t"=>"-",
            "u"=>"..-",
            "v"=>"...-",
            "w"=>".--",
            "x"=>"-..-",
            "y"=>"-.--",
            "z"=>"--..",
            "0"=>"-----",
            "1"=>".----",
            "2"=>"..---",
            "3"=>"...--",
            "4"=>"....-",
            "5"=>".....",
            "6"=>"-....",
            "7"=>"--...",
            "8"=>"---..",
            "9"=>"----.",
            "."=>".-.-.-",
            ","=>"--..--",
            "?"=>"..--..",
            "/"=>"-..-.",
            " "=>" ");
    
    
        $possible_match = array();
        foreach ($possibilities as $key => $value) {
            if (preg_match($regexp, $value)) {
                array_push($possible_match, $key);
            }
        }
        return $possible_match;
    }
    
    print_r(find_possible_match(".-")); // ['a']
    print_r(find_possible_match("?")); // ['e','t']
    print_r(find_possible_match("?.")); // ['i','n']
    print_r(find_possible_match(".?")); // ['i','a']
    

    Outputs:

    Array
    (
        [0] => a
    )
    Array
    (
        [0] => e
        [1] => t
    )
    Array
    (
        [0] => i
        [1] => n
    )
    Array
    (
        [0] => a
        [1] => i
    )