仅当数组中的某个单词写入文本框时,如何递增数字

时间:2017-12-22 03:27:08

标签: php

我的网站是用PHP编写的,这是我有的代码

//good words
        $body = implode(" ", $body_array);
        $niceWords = array("nice", "amazing", "fantastic", "great");
        $body = preg_replace('/\b('.implode('|',$niceWords).')\b/','You have used the speical ',$body);

        //Start the Points
        $_SESSION['points'];
        $_SESSION['points']++;
        echo $_SESSION['points'];

1 个答案:

答案 0 :(得分:0)

    <?php session_start(); ?>
    <form method="POST">
        <input type="text" size="60" name="myText" id="myText" />
        <input type="submit" name="submit" value="submit" />
    </form>      

    <?php

    if(isset($_POST["submit"])) {            
        //echo "$_POST[myText] <br>";            

        $niceWords = array("nice", "amazing", "fantastic", "great");            
        $textArray = explode(" ", $_POST["myText"]); // create an array of all the words from the textbox

        if(! isset($_SESSION["points"])) {
            //Start the Points
            $_SESSION["points"] = 0;
        }

        foreach($textArray as $word) {
            //echo "$word <br/>";
            // check if the word is in the array of niceWords
            if(in_array($word, $niceWords)) {
                //echo "Yeah!! nice word... <br/>";
                // if it is in the array increment points counter
                $_SESSION["points"]++;
            }
        }
        echo $_SESSION['points'];

    }
    ?>