How to remove some line from a text file using PHP?

I have a text file containing this data:

947 11106620030 Ancho Khoren MKK6203 Introduction Busy 2.00
948 balblalbllablab
949 balblalbllablab
950 balblalbllablab
951 11106620031 Adagasa Goo MKB6201 Economy Inside 3.00
952 balblalbllablab
953 balblalbllablab
954 balblalbllablab
962 11106620032 Fumiou Moon MKB6201 Book of World 3.00

However, I need to delete all rows containing "balblabllablab" and just leave tge specific data rows, as shown below:

947 11106620030 Ancho Khoren MKK6203 Introduction Employed 2.00
951 11106620031 Adagasa Goo MKB6201
Economy Inside 3.00
962 11106620032 Fumiou Moon MKB6201 World Book 3.00

I know how to open and write to a file, as well as close a file, but I don't know how to delete lines / contents using php. How to remove unwanted lines from a file using php?

+1
source share
2 answers

Use a function file($path)to get the rows in an array, then scroll through it.

$lines = file($path, FILE_IGNORE_NEW_LINES);
$remove = "balblalbllablab";
foreach($lines as $key => $line)
  if(stristr($line, $remove)) unset($lines[$key]);

$data = implode('\n', array_values($lines));

$file = fopen($path);
fwrite($file, $data);
fclose($file);
+9
source

The content in the file is fuzzy, what exactly do you want to take from a text file?

Usually, using a regular expression, you can get the required content only if it has a common template.

, .

refer: http://php.net/manual/en/function.preg-match.php

+1

Source: https://habr.com/ru/post/1605794/


All Articles