Deleting line from a string by finding specific part of string PHP

Solution:1

If explode() is banned, dare I suggest using regular expressions?

$string = preg_replace('/\nxyz.*/', '', $string);

This will match a line beginning with xyz and delete up to the next line break.

Solution:2

There are at least 2 ways:

Fast:

  • find “\nxyz” with strpos() function and store the position as start
  • find find \n after the start position
  • get substring usnig substr substr() and store it
  • repeat
  • cut substrings using str_replace(substrs_array, '', text)

Handy:

use Regular Expressions $text = preg_replace('/^xyz.*$/g', '', $string);