0

I need help with my PHP website. I need to search if textbox includes some text. Its working only on half. When I type "Kdy" it works but when I type "Kdy prijdes" it wont work. I need to change output when textbox includes some part of textbox but my idea wont work. Any solutions?

<h1>Zeptej se mě</h1>
<form id="frm" method="POST"  action="?action">
<input type = "text" name = "textbox" value = ""/>
    <input type="submit" value="Odešli" id="submit"/>
</form>

<?php
if(isset($_GET['action']))
{
    $text = $_POST['textbox'];

    $kde = array("kde", "Kde");
    $kam = array("kam", "Kam");
    $kdy = array("kdy", "Kdy");
    $jak = array("jak", "Jak");
    $co = array("co", "Co");
    $proc = array("proč", "proc", "Proč", "Proc");
    $kdo = array("kdo", "Kdo");
    $koho = array("koho", "Koho");
    
    if (in_array($text, $kde))
    {
        echo "Nikde";
    }
    elseif(in_array($text, $kam))
    {
        echo "Nikam";
    }
    elseif(in_array($text, $kdy))
    {
        echo "Nikdy";
    }
    elseif(in_array($text, $jak))
    {
        echo "Nevim";
    }
    elseif(in_array($text, $co))
    {
        echo "Nic";
    }
    elseif(in_array($text, $proc))
    {
        echo "Nevim";
    }
    elseif(in_array($text, $kdo))
    {
        echo "Nikdo";
    }
    elseif(in_array($text, $koho))
    {
        echo "Nikoho";
    }
    else
    {
        $text = array("Spíš ne", "Asi", "No nevím");
        echo $text[array_rand($text)];
    }
}
?>
3
  • 1
    in_array is the wrong function for what you're trying to do. strpos is the way to go. Commented Dec 15, 2020 at 13:29
  • Can you expand on "Won't work"? When you type in two words like that, do you want it to return search results for either of those two words, or only if they both are found? If you want to search on either word, look at explode() to split the text field into separate words and search for each of them. Commented Dec 15, 2020 at 13:30
  • @droopsnoot Only if one is found. My language is little confusing i know :D so if you type "Why" it anser "I dont know". But when u type "Why are you running" it type something else. Do you understand? Commented Dec 15, 2020 at 13:37

2 Answers 2

1

I would try to find a solution for this using strpos()

so if you check for a specific part of text it could look like this:

if(strpos($text, "Kdy") !== false){
//do something
}

Another approach could be to explode() the $text into its parts and compare the array with an array of strings you want to check for.

But there might be smarter solutions for this.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use next simple function:

<?php

function find_text($text) {
    // convert input text to lower case
    $text = strtolower($text);

    // build dictionary with responses on each word 
    $dict = [
        "kde" => "Nikde",
        "kam" => "Nikam",
        "kdy" => "Nikdy",
        "jak" => "Nevim",
        "co" => "Nic",
        "proč" => "Nevim",
        "proc" => "Nevim",
        "kdo" => "Nikdo",
        "koho" => "Nikoho",
    ];

    // loop trough dictionary
    foreach($dict as $find=>$response) {
        if (false !== strpos($text, $find)) {
            // immediately return response when one of dictionary words found 
            return $response;
        }
    }
    // if input cot contains no one of dictionary words return 'not found'
    $text = array("Spíš ne", "Asi", "No nevím");
    return $text[array_rand($text)];
};

// echo find_text('"Kdy prijdes"');

if(isset($_GET['action']))
{
    echo find_text($_POST['textbox']);
}
?>

You can test PHP code here

2 Comments

bro can you explain me how it works? I dont understand this so much :D
Code comments added

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.