在文本中搜索单词并突出显示

时间:2016-12-31 01:12:49

标签: php html

我正在尝试编写一个PHP脚本,它将文本作为输入,遍历整个文本(例如,它可能是一篇很长的文章),并在表单中搜索单词user inputs。

它应该用用户正在搜索的突出显示的单词打印出整个文本,无论它在哪里找到。

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Search for a word in text</title>
</head>

<body>

    <form method="post" action="index.php">
        <input type="text" name="text" placeholder="Insert the text you want to search in...">
        <input type="text" name="find_x" placeholder="Insert the word you want to find...">
        <input type="submit" value="Search">
    </form>

    <?php

        // this just prints out a line used to devide input area from a result
        $line = "<br>" . str_repeat("___", 40) . "<br></br>";
        echo $line;

        if ( isset($_POST["text"]) and !empty($_POST["text"]) and 
             isset($_POST["find_x"]) and !empty($_POST["find_x"]) ) 
        {
            $text = $_POST["text"];
            $find_x = $_POST["find_x"];

            // transforms inputs to lowercase so the word is found even if written in uppercase  
            $text_lc = strtolower($text);
            $find_x_lc = strtolower($find_x);

            // find length of searched word
            $length_of_x = strlen($find_x_lc);

            // variable for offsetting
            $offset = 0;

            // prints out all starting positions of a searched word
            while ( $position_in_text = strpos($text_lc, $find_x_lc, $offset) ) 
            {
                echo "The string <b><u>$find_x</u></b> is found in your text and it is at position $position_in_text" . "<br>";
                $offset = $position_in_text + $length_of_x;
            }


            // here I want to print the whole text, and wherever the searched word appears in the text it should be highlighted (bold or underlined). I don't know how to do that!?

        }

    ?>

</body>

1 个答案:

答案 0 :(得分:0)

您可以使用条带,而不是使用strpos,这样您也可以保持区分大小写。另请注意,如果搜索文本位于位置0,while( false !== stripos() )可能甚至不会运行,这意味着它位于字符串的开头,因此需要检查stripos / strpos是否将返回false(如果不匹配则为false)已被发现。)

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Search for a word in text</title>
</head>

<body>

    <form method="post" action="index.php">
        <input type="text" name="text" placeholder="Insert the text you want to search in...">
        <input type="text" name="find_x" placeholder="Insert the word you want to find...">
        <input type="submit" value="Search">
    </form>

    <?php

        // this just prints out a line used to devide input area from a result
        $line = "<br>" . str_repeat("___", 40) . "<br></br>";
        echo $line;

        if ( isset($_POST["text"]) and !empty($_POST["text"]) and
             isset($_POST["find_x"]) and !empty($_POST["find_x"]) )
        {
            $text = $_POST["text"];
            $find_x = $_POST["find_x"];

            // transforms inputs to lowercase so the word is found even if written in uppercase
            //~ $text_lc = strtolower($text);
            //~ $find_x_lc = strtolower($find_x);

            // find length of searched word
            //~ $length_of_x = strlen($find_x_lc);
            $length_of_x = strlen($find_x);

            // variable for offsetting
            $offset = 0;

            // prints out all starting positions of a searched word
            while ( false !== ( $position_in_text = stripos($text, $find_x, $offset) ) )
            {
                $found_string = substr( $text, $position_in_text, $length_of_x );

                echo "The string <b><u>", htmlspecialchars( $found_string ), "</u></b> is found in your text and it is at position $position_in_text" . "<br>";
                // maybe even sanitizing or escaping some of the texts? XSS here
                //~ $replacement = sprintf( '<strong>%s</strong>', htmlspecialchars( $found_string ) );
                $replacement = sprintf( '<strong>%s</strong>', $found_string );
                $text = substr_replace( $text, $replacement, $position_in_text, $length_of_x );
                $offset = $position_in_text + strlen( $replacement );
            }

            echo $line, $text;             
        }

    ?>

</body>
Text:
hello world HELLO again and good night heLLo

Search string:
hello

Expected Result should look like:
<strong>hello</strong> world <strong>HELLO</strong> again and good night <strong>heLLo</strong>