2

I have the following text block containing names in their own line:

Sarah Jane Smith
Archie Jones
Micky Smith
Rose Tyler
Harriet Jones
Jack Harkness
John Smith
Martha Jones
Donna Noble

Using PHP, I want to delete all the lines that do NOT contain Jones and leave the lines containing Jones, as follows:

Archie Jones
Harriet Jones
Martha Jones

There are several answers that show how to delete lines that contain a word, but no answers that provide a method of deleting all those that don't contain the key string.

Jonah Kyle
  • 31
  • 3

2 Answers2

3

The way I would do this is to split the text block into an array with an entry for each line:

$lines = explode(PHP_EOL, $text);

Then filter out the lines that don't include the name you want:

$remaining = array_filter($lines, function($line) {
     return strpos($line, 'Jones') !== false;
});

Then you can rejoin the array with the values you want

echo implode(PHP_EOL, $remaining);
jfadich
  • 6,110
  • 2
  • 20
  • 31
  • 1
    Keep in mind that this only works for arrays that can fit into memory. If you have huge file you need another approach. – Boy Feb 21 '19 at 23:19
  • @Boy Yes that's a good point. This should work for medium to small data sets. For anything larger OP would need to either look into reading the names in as a stream or save it to a file and go line by line using something like `fgets` or `stream_get_line` – jfadich Feb 21 '19 at 23:30
  • I have a personal hosting account, and my file is rarely over 60,000 lines. This is precisely what I need, so this is an excellent answer. I consider the answer SOLVED. Excellent answer, jfadich. – Jonah Kyle Feb 22 '19 at 01:46
1

You can use a regular expression to match all the lines that do contain Jones, then combine the matches.

preg_match_all('/.*Jones.*/m', $text, $matches);
echo implode("\n", $matches[0]);
Don't Panic
  • 41,125
  • 10
  • 61
  • 80