Error when searching for letters in a string with PHP

0

I'm building a college project, a lexical analyzer.

I was able to set up the entire structure, but my teacher asked me to click on "space" to continue searching for letters.

My problem, if the word is the initial, it searches without errors, the problem is that by clicking on the space and adding a new letter to search in my dictionary, this does not change the position.

My code: link

I think my error is in: else if ($ i! = Intval (strlen ($ word)) - 1)

Example: I have two inputs, one to add the word to the dictionary, and another to perform the letter search.

I get an input via $ _POST the words: ball, car where I record "bola+carro" or "bola carro" . Where do I write these words in an array.

Now in my other entry I first look for the word ball, based on the position, the ball has 4 letters or if it reaches 4 it becomes final. So far so good and it works, the problem is, if I give a space instead of going to position 5 for later I type "c" and go to position 6 and so on it simply stops at position 5 or in space and so I can not get the word "car".

I do not know if I'm very clear, but the code explains it more simply. Code:

<?php
$word = $_POST["word"];
$words = $_POST["words"];
$verifyProccess = $_POST["verifyProccess"];

$alfabet="abcdefghijklmnopqrstuvwxyz";
$linePosition = 1;
$maxLengMatrizTable = array(array(500), array(500));
$vecTable = array(500);
$initSizeMatrizTable = 0;

$symbols = explode(" ", $words);

for ($i = 0; $i < sizeof($symbols); $i++) {
    $trim = rtrim($symbols[$i], " ");
    $initSizeMatrizTable = $initSizeMatrizTable + strlen($trim);
}

$initSizeMatrizTable = $initSizeMatrizTable + 3;

for ($i = 1; $i < $initSizeMatrizTable; $i++) {
    for ($j = 0; $j < 27; $j++) {
        $maxLengMatrizTable[$i][$j] = "-";
    }
}

$lastLine = 1;
$totalLine = 1;
$nextLine = 1;
$cont = 0;

/* Adiciona a word na tabela passando as posicoes corretamente(monta a matriz já com as posições corretas) */
for ($i = 0; $i < sizeof($symbols); $i++) {
    $nextLine = 1;
    $trim = rtrim($symbols[$i], " ");

    for ($j = 0; $j < intval(strlen($trim)); $j++) {
        $alfabetPos = strripos($alfabet, $trim[$j]) + 1;

        if ($maxLengMatrizTable[$nextLine][$alfabetPos] == "-") {
            $lastLine = $lastLine + 1;
            $maxLengMatrizTable[$nextLine][$alfabetPos] = $lastLine;
            $totalLine = $totalLine + 1;
        }
        /* ultima posicao da palavra*/              
        $nextLine = $maxLengMatrizTable[$nextLine][$alfabetPos];

        if (intval(strlen($trim) - 1) == $j) {
            $vecTable[$cont] = $lastLine;
            $cont = $cont + 1;
        }
    }
}

$sizeMatriz = $totalLine + 1;
$errorPosition = $sizeMatriz - 1;

/* SEARCH! pegar da outra page */
/* faz a quebra da palavra */
for ($i = 0; $i < intval(strlen($word)); $i++) {
    $alfabetPos = strpos($alfabet, $word[$i]) + 1;

    if ($word[$i] != " ") {
        if ($alfabetPos != " " && $verifyProccess == true) {
            if ($maxLengMatrizTable[$linePosition][$alfabetPos] != "-" && $verifyProccess) {
                $verifyProccess = true;

                /* Define a proxima linha que ira pintar */
                $linePosition = $maxLengMatrizTable[$linePosition][$alfabetPos];

                $answer = array(
                    "linePosition"  =>  $linePosition,
                    "wordProccess"  =>  $verifyProccess
                );

            } else {
                $verifyProccess = false;

                $answer = array(
                    "linePosition" => $errorPosition,
                    "wordProccess" => $verifyProccess
                );
            }
        } else {
            $verifyProccess = false;

            $answer = array(
                "linePosition" => $errorPosition,
                "wordProccess" => $verifyProccess
            );
        }
    } else if ($i != intval(strlen($word)) - 1) {
        $final = false;

        for($j = 0; $j < sizeof($vecTable); $j++) {
            if($vecTable[$j] == $linePosition) {
                $final = true;
            }
        }
    } else {
        //
    }
}

$jsonanswer = json_encode($answer);
echo $jsonanswer;

Thank you!

    
asked by anonymous 26.11.2018 / 14:26

0 answers