str_replace仅匹配

Good Day,

I am trying to create a morse code to text and text to morse code converter.

My code:

$letter = str_split(strtolower($_POST['text']));
$morse = $_POST['morse'];
$morsecmp = explode(" ",$morse);
$letter = implode(" ",$letter);
$mode = $_POST['sub'];

$morsecode = array(".-","-...","-.-.","-..","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",
            ".--.","--.-",".-.","...","-.","..-","...-",".--","-..-","-.--","--..",".");
$letters = array("a","b","c","d","f","g","h","i","j","k","l","m","n","o","p","q","r",
            "s","t","u","v","w","x","y","z","e");

if($mode == "Text to Morse Code"){  
    $letter = str_replace($letters,$morsecode,$letter);
    $translated = $letter;
}else{
    for($x=0;$x<sizeof($letters);$x++){
        for($y=0;$y<sizeof($morsecmp);$y++){

            if($morsecode[$x] === $morsecmp[$y]){
                echo $morsecode[$x]." === ".$letters[$x]."<br>";
                $morse = str_replace($morsecode[$x],$letters[$x],$morse);
            }
        }
    }
    $translated = $morse;
}

sample input:

.... . .-.. .-.. --- .-- --- .-. .-.. -..

sample output:

h e ed ed o w o r ed d

expected output:

hello wolrd

My problem is that when converting from morse code to text some characters are not captured properly due to str_replace limit where it will replace all string that is similar to the needle, so if i have to replace all "." to e it will also change "...." which should be actually an h.

any help on this would be greatly appreciated.

Thank You.

Just when you repalce the characters add an extra space for the search string.

 $morse = $_POST['morse']." ";  // this is to add an extra space at the end of the morse string.

Now we replace all occurrences of morse code strings followed by space with the desired letter.

 $morse = str_replace($morsecode[$x]." ",$letters[$x],$morse);

First off, you don't have the full alphabet stored in your array. I noticed you're missing 'e'.

Blow the morse code into an array with explode(' ', $morse_code) and then do a replace on the array (use '/' to delimit words).

Condense it back to a string with implode().

heres an array for you to use, its got the whole alphabet (you can use array_flip to switch the keys and values for translating back and forth)

$translator_table = 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' => '----.',
'.' => '.-.-.-',
',' => '--..--',
'?' => '..--.',
);

The only way to replace exact matches is:

   $morse = preg_replace("#{$morsecode[$x]}#", $letters[$x], $morse, 1); //Limit to 1