0

I am trying to make a if that will try to get a line of a document by searching after it. So it searches for "Registrar Handle" in the document and get the whole output of the line.

The problem I have is that is that I get this error: Notice: Undefined index: Registrar Handle in C:\xxxx\xxxx\xxx\xx on line 29

Would be nice if someone had a solution for this and maybe have an explanation for why it is happening?

$commandR = "whois ".mysql_result($domain, 0);  

$outputR = shell_exec($commandR);
$fhR = fopen('R.txt','w+');
fwrite($fhR,$outputR);
fclose($fhR);
  { 
    //.no
    if( strpos(file_get_contents("./R.txt"),$_GET['Registrar Handle'])){
       $fhR = fopen('A.txt','r');
       $R = fgets($_GET);
       fclose($fhA);
       Echo $R;
       //mysql_query("UPDATE `dom_oversikt`.`server1` SET `Registrar`='".$R."' WHERE `id` = '$id';");      
    }
Leifus
  • 41
  • 9
  • And what problem do you have? – u_mulder Jan 22 '16 at 12:27
  • @u_mulder i updated it a little – Leifus Jan 22 '16 at 12:30
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – u_mulder Jan 22 '16 at 12:31
  • Why are you writing the output of a command to a file, only to read it again? You already had it in a variable. Also, I don't see any point in using the `$_GET` variable there. – Gerald Schneider Jan 22 '16 at 13:05
  • Note: The `mysql_*` functions are deprecated, they have been removed from PHP 7, your code will stop working when you upgrade to that version. You should not write new code using them, use [`mysqli_*` or PDO](http://php.net/manual/en/mysqlinfo.api.choosing.php) instead. – Gerald Schneider Jan 22 '16 at 13:06
  • Heard of regular expression ? –  Jan 22 '16 at 13:09
  • You should use a regular expression, can you post your file content ! – JC Sama Jan 22 '16 at 13:14

1 Answers1

0

You can iterate over the file you just created, get each line of the file until you reach the end of it and echo/return the line that contains the string you mentioned.

$searchedString = 'Registar Handle';
if (($handle = fopen("./R.txt", "r")) !== false) {
    while (!feof($handle)) {
        $currentLineInFile = fgetss($handle, 4096);
        if(false !== strpos($currentLineInFile, $searchedString)){
            echo $currentLineInFile;
        }
    }

    fclose($handle);
}

One could argue that you can use preg_match instead of strpos, but it's more complex and less fast (according to that SO answer pointing to the php doc itself).

Regarding your error, it's only due to the fact that there is no key 'Registar Handle' in the array $_GET.

Community
  • 1
  • 1
Lex Lustor
  • 1,525
  • 2
  • 22
  • 27