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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化