简体   繁体   中英

PHP Simple Search Example

I'm learning PHP and built a little search in text file script. Here is the chain of events:

  1. Opens text file
  2. Creates array from each new line
  3. If form is submitted, the post data is put into a function runs a regex to find the partial string or word and if matched, all entries are replaced in code with a class around, with some CSS.

My issue is that:

If I have a sentence like:

Tom has a camera in his hand. I am also a camera on the floor.

And search for "am", it searches and finds camera, replaces this with the span class. Then it finds "am" and replaces it within Camera twice, and itself.

Here is my code:

    //---- Puts Array into or responds with error
    $lines = file($filename, FILE_IGNORE_NEW_LINES);
    if ( !is_array($lines) ) {
       echo 'Cannot create array from file';
    }

    //---- Scans and Displays Lines
    $submission = $_GET['Search'];
    function scanLines($lines, $submission) {
        foreach($lines as $line) {      
            if(strpos($line,$submission)!== false){             
                preg_match_all("/\b[a-z]*".preg_quote($submission)."[a-z]*\b/i", $line, $matches);  

                $i = 0;
                foreach($matches[$i] as $match) {   
                    $line = str_replace($match, '<span class="highlight">'.$match.'</span>', $line);
                $i++;                   
            }
            echo $line;
        }       
    }       
}

Direct link to example: http://www.effectivemark.com/testphp/

My question is: What is the best approach to filter out the "am" in camera, so it doesn't add the string with span tag like below:

    <span class="highlight">c<span class="highlight">am</span>era</span>

Updated code:

//---- Scans and Displays Lines
$submission = $_GET['Search'];
function scanLines($lines, $submission) {
    foreach($lines as $line) {      
        if(strpos($line,$submission)!== false){ 

            $words = explode(' ', $line);
            $i = 0;
            $combine = array();
            foreach($words as $word) {
                if (preg_match("/\b[a-z]*".preg_quote($submission)."[a-z]*\b/i", $word)) {
                    preg_match_all("/\b[a-z]*".preg_quote($submission)."[a-z]*\b/i", $word, $match);
                    $matches = '<span class="highlight">' . $match[0][0] . '</span>';
                }
                else { 
                    $matches = $word;
                }
                array_push($combine, $matches);
            $i++;
            }

            foreach($combine as $combined) {
                echo $combined . ' ';
            }
        }       
    }       
}

你最好把行分成单词,这样可以很容易地匹配它们

I think this would be ok:

    <form action="" method="post">
    <h4>Search Object In Text File:</h4>
    <input type="text" name="search" value="">
    <input type="submit" value="Submit" name="Submit">
    </form>
    <?php
        $filename="tekst.txt";
        $lines = file($filename, FILE_IGNORE_NEW_LINES);

        if ( !is_array($lines) ) {
            echo 'Cannot create array from file';
        }

        //---- Scans and Displays Lines

        if(isset($_POST['search']))    
$submission = $_POST['search'];
        scanLines($lines,$submission);

        function scanLines($lines, $submission) {
            foreach($lines as $line) {
                $word_arr=str_word_count($line,1);
                $length=count($word_arr);
                for($i=0;$i<$length;$i++) {

                    if(preg_match("#$submission#",$word_arr[$i],$match)) {
                        $word_arr[$i]='<span class="highlight">'.$word_arr[$i].'</span>';
                        $new_line=implode(' ',$word_arr);
                        echo $new_line.'<br>';
                    }

                }

            }

        }

        ?>

So, if you want to search FOR ANY PART OF THE WORD, and highlight whole word, rather than just matched part. For complete (more rigid) whole word search, you can just use simple in_array() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM