Convert words and phrases to anagram

    Anagram (from the Greek. Ανα- - “per” and γράμμα - “letter”) is a literary device consisting in rearrangement of letters or sounds of a certain word (or phrases), which results in another word or phrase
    Anagrams are used to encrypt the answer to a quiz question (charade, crossword, etc.).

    I bring to your attention two functions that implement the algorithm for obtaining anagrams from individual words and phrases.

    The work of the functions "in combat", as they say, can be seen here .

    To select the most “mixed” anagram, the Levenshtein function is used.
    For words with a length of three letters and less, an anagram is not composed - there is no sense.
    For numbers - the same.
    Encoding is UTF-8.

    As the value of the only parameter of the GetAnagramm function, we submit a word or phrase, and at the output of GetWordAnagramm we get a ready-made anagram in which all the letters are enclosed in HTML SPAN elements (in order to give the anagram a presentable look).

    //Получение анаграммы слова или словосочетанияfunctionGetAnagramm($anagramm){
        $out_anagramm = "";
        $len_anagramm = mb_strlen($anagramm,'UTF-8');
        $int_anagramm = (int)$anagramm;
        if ($len_anagramm > 3 && $int_anagramm == 0)
        {
            mb_regex_encoding('UTF-8');
            mb_internal_encoding('UTF-8');
            $wordslist = preg_split('[-| ]', $anagramm);
            $out_anagramm = "";
            $len_anagramm = 0;
            $i = 0;
            foreach ( $wordslist as $value)
            {
                $len_anagramm = $len_anagramm + mb_strlen($value,'UTF-8');
                $simbol = mb_substr($anagramm, $len_anagramm+$i, 1, 'UTF-8');
                $span = "";
                if ($simbol <> "") $span = "<span class=\"annagramm\">".$simbol."</span> ";
                if (mb_strlen($value,'UTF-8') == 1)
                {
                    $out_anagramm .= "<span class=\"annagramm\">".$value."</span> ".$span;
                }else{
                    $out_anagramm .= GetWordAnagramm($value).$span;
                }
                $i++;
            }
        }
        return $out_anagramm;
    }
    //Получение аннаграммы отдельного словаfunctionGetWordAnagramm($anagramm){
        $array_an = preg_split('//u',$anagramm,-1,PREG_SPLIT_NO_EMPTY);
        $j = 0;
        $maxLeven = 0;
        while ($j < 10)
        {
            srand((float)microtime() * 1000000);
            shuffle($array_an);
            $an = "";
            $ot = "";
            $i = 0;
            foreach ( $array_an as $value )
            {
                $an .= $value." ";
                $ot .= mb_substr ($anagramm, $i, 1, 'UTF-8')." ";
                $i++;
            }
            $leven = levenshtein ($an,$ot);
            $j++;
            if ($leven > $maxLeven)
            {
                $maxLeven = $leven;
                $best_array_an = $array_an;
            }
        }
        $an_div = "";
        foreach ( $best_array_an as $value )
        {
            $an_div .= "<span class=\"annagramm\">".$value."</span> ";
        }
        return $an_div;
    }
    

    If you add the CSS style for the annagramm SPAN class to the output page:

    span.annagramm {
        background-color: #ffffff;
        border-style: outset;
        border-width: 1px;
        border-color: #cccccc;
        -webkit-border-radius: 4px;
        -moz-border-radius:4px;
        border-radius: 4px;
        padding: 4px;
        padding-left: 6px;
        padding-right: 6px;
        margin-left: 1px;
        margin-right: 1px;
        margin-top: 6px;
        margin-bottom: 6px;
        font-weight: bold;
        color: #4f4ba8;
        font-size: 11pt;
        text-transform: uppercase;
        box-shadow: 01px4pxrgba(0, 0, 0, .3), -23px020px -23pxrgba(0, 0, 0, .8), 23px020px -23pxrgba(0, 0, 0, .8), 0040pxrgba(0, 0, 0, .1) inset;
    }
    

    then we will get this “beauty”: The

    image

    correct answer: Agafon Nikitin .

    Also popular now: