Home > Software design >  Batch search in php txt file
Batch search in php txt file

Time:06-17

I want to search the ip addresses in the txt file. A single ip address shows the result when I log in, but when I type more than one ip address in the textarea, the result is not found. Can anyone help?

<html> 
<head> 
    <meta charset="utf-8" /> 
    <title>search</title> 
</head> 
<body> 
    <form method="POST"> 
        <textarea type="text" name="q" id="q"></textarea> 
        <button type="sumbit">ok</button> 
    </form> 

    <?php if($_POST){ 
        $file = 'check.txt'; 
        $find = $_POST['q']; 
        $contents = file_get_contents($file); 
        $pattern = preg_quote($find, '/'); 
        $pattern = "/^.*$pattern.*\$/m"; 
        if(preg_match_all($pattern, $contents, $matches)){ 
           echo implode("\n", $matches[0]); 
        } 
        else{ 
           echo "error"; 
        }} ?> 
</body> 

Sample Check.txt

127.0.0.1
44.222.223.22
252.241.351.24
55.244.241.2
241.212.235.42

CodePudding user response:

If everything is working good then for more than one ip address. your if condition should be like below:

if (preg_match_all($pattern, $contents, $matches)){
    $matches = array_unique($matches[0]);
    foreach($matches as $match){
        echo "<pre>$match</pre>";
    }
}

CodePudding user response:

I think filter_var() function, can do the job fast and keep code simple.

Simple, workable solution to improve:

 <?php
    echo'<hr>';
    // Get list from txt file and make an array
    $IP_list=explode(PHP_EOL,file_get_contents('Check.txt'));
    $IP_count=count($IP_list);
    // Get data from textarea and seperate to "words" expecting that every word can be IP
    $IP_input=$_POST['q'];
    $IP_to_check_list=explode(PHP_EOL,$IP_input);
    $IP_to_check_count=count($IP_to_check_list);
    // Loop trough array of "words" from textarea
    for($index_search_for=0;$index_search_for<$IP_to_check_count;$index_search_for  )
    {
        // Check if "word" is IP
        if(filter_var($IP_to_check_list[$index_search_for], FILTER_VALIDATE_IP))
        {
            // If "word" is IP, try to find it in text file
            $found=0;
            for($index_search_at=0;$index_search_at<$IP_count;$index_search_at  )
            {
                if($IP_to_check_list[$index_search_for]==$IP_list[$index_search_at])
                {
                    // If match found - show result and stop loop
                    echo '<strong>'.$IP_to_check_list[$index_search_for].'</strong> is in list<br>';
                    $found=1;
                    break;
                }
            }
            if($found==0)
            {
                echo $IP_to_check_list[$index_search_for].' - no matches<br>';
            }
        }
        else
        {
            echo $IP_to_check_list[$index_search_for].' - not an IP<br>';
        }
    }
    echo'<hr>';
    echo'<form method="POST"><textarea name="q" id="q"></textarea><input type=submit value="Ok"></form>';
    ?>

CodePudding user response:

Unfortunately, when more than one ip logs in, it does not match. enter image description here

  • Related