$tmpfile = "/var/tmp/teste"; $fw = fopen($tmpfile,"w"); $fp = fopen("/etc/resolv.conf","r"); $fw = fopen($tmpfile,"w"); while(!feof($fp)) { $line_x = fgets($fp); if (preg_match($search,$line_x)) { $line = preg_replace($search,$replace,$line_x); } else { $line = $line_x; } fwrite($fw,$line); } ..... root@ubwifidog:/tmp# cat /etc/resolv.conf nameserver 200.xxxxxxx nameserver 200.xxxxxxx domain test.com root@ubwifidog:/tmp# but in /var/tmp/teste root@ubwifidog:/tmp# cat /var/tmp/teste nameserver 200.xxxxxxx nameserver 200.xxxxxxx domain test.com root@ubwifidog:/tmp# I have blank line, how to solve this ?