1. You can read from source line by line, if a line should not be deleted write it to target file. After that rename target file into a source one. This is the right solution.
2. You can read all strings into array with file(), remove certain strings, then write an array to file. This is lazy solution and should be not performed on large files.