doudanma9706 2018-04-12 11:48 采纳率: 100%
浏览 90
已采纳

匹配单个未知参数php(莫尔斯码正则表达式)

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

  • 写回答

2条回答 默认 最新

  • doujia1939 2018-04-12 12:33
    关注

    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
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器