Right, as you are reading the file each time then put the check in the read loop, you can then stop the read loop when you find a match.
$handle = fopen("/home/rasmus/file.txt", "r");
While (!feof($handle)){
$line = fgets($handle);
$field = explode('|', trim($line));
if($_POST['action'] == "search" && $_post['key'] == $field[0]) {
echo $field[1];
}
}
However, if the file is large and it is used over and over again consider dumping into a database and using queries. It would make a lot more sense.