Triggers in PHP

1

I would like to know how I can create an activator - or trigger - in PHP but I do not know where to start.

I wanted to make the user type, for example, "born stack". PHP would recognize the first term "NATO" and would result in "Sierra-Tango-Alpha-Charlie-Kilo".

I've researched it and it's called the Trigger, or activator in Portuguese.

My template for creation is this: link

Issue 1:
I got it from here with PHP:

<?php
$busca = strtolower('nato StACKoverFLoW');
$nato = str_replace("nato", "", $busca);
$trans = array("a" => "Alfa ", "b" => "Bravo ", "c" => "Charlie ", "d" => "Delta ", "e" => "Echo ", "f" => "Foxtrot ", "g" => "Golf ", "h"  => "Hotel ", "i" => "India ", "j" => "Juliet ", "k" => "Kilo ", "l" => "Lima ", "m" => "Mike ", "n" => "November ", "o" => "Oscar ", "p" => "Papa ", "q" => "Quebec ", "r" => "Romeo ", "s" => "Sierra ", "t" => "Tango ", "u" => "Uniform ", "v" => "Victor ", "w" => "Whiskey ", "x" => "Xray ", "y" => "Yankee ", "z" => "Zulu ", "1" => "One ", "2" => "Two ", "3" => "Three ", "4" => "Four ", "5" => "Five ", "6" => "Six ", "7" => "Seven ", "8" => "Eight ", "9" => "Nine ", "0" => "Zero");
echo strtr($nato, $trans);
?>
    
asked by anonymous 14.04.2015 / 02:54

1 answer

1

I believe you can implement this with regular expression, here's an example:

<?php
$regex = '/nato (?<nato>[a-z0-9 ]+)/';

$vocabulario = array('a' => "Alfa", 'b' => "Bravo", 'c' => "Charlie", 'd' => "Delta", 'e' => "Echo", 'f' => "Foxtrot", 'g' => "Golf", 'h' => "Hotel", 'i' => "India", 'j' => "Juliet", 'k' => "Kilo", 'l' => "Lima", 'm' => "Mike", 'n' => "November", 'o' => "Oscar", 'p' => "Papa", 'q' => "Quebec", 'r' => "Romeo", 's' => "Sierra", 't' => "Tango", 'u' => "Uniform", 'v' => "Victor", 'w' => "Whiskey", 'x' => "Xray", 'y' => "Yankee", 'z' => "Zulu", '1' => "One", '2' => "Two", '3' => "Three", '4' => "Four", '5' => "Five", '6' => "Six", '7' => "Seven", '8' => "Eight", '9' => "Nine", '0' => "Zero");

$entrada = 'minha frase com nato stack overflow';

$temComandoNato = preg_match($regex, strtolower($entrada), $encontrados);

if($temComandoNato) {
    $letras = str_split($encontrados['nato']);

    $natos = array_map(function($letra) use($vocabulario){
        return  @$vocabulario[$letra];
    }, $letras);

    echo implode('-', $natos);
}

Simplified parsing with string handling functions, the difference in output is that it does not do dash dash "-" when it finds space.

if($temComandoNato) {
    $text = $encontrados['nato'];
    $text = wordwrap($text, 1, '-', true);
    $text = strtr($text, $vocabulario);
    echo $text;
}
    
14.04.2015 / 03:50