dongpang2483 2016-12-31 01:12
浏览 63
已采纳

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

I am trying to write a php script that takes a text as an input, goes through the whole text (for example it can be a very long essay), and search for the word user inputs in the form.

It should print out the whole text with the highlighted word user was searching for, wherever it was found.

<!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条回答 默认 最新

  • douchao1864 2016-12-31 02:22
    关注

    Instead of using strpos you could use stripos, this way you can keep the case sensitivity as well. Also note the while( false !== stripos() ) may not even run if the search text is at position 0, meaning that it's at the start of the string, thus the need to check if stripos/strpos will return false or not ( false if no match had been found ).

    <!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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误