Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Deekshith's avatar

Split the string to array based on seperator

i have a html editor and i have a data like below,

<p>Author Name, Department</p>

<p>##SEPERATOR##</p>

<p>Author Name, Department</p>

<p>##SEPERATOR##</p>

<p>Author Name, Department</p>

<p>##SEPERATOR##</p>

<p>Author Name, Department</p>

Here i am trying to explode above data using the separator ##SEPERATOR## keyword.

i have a code like below,

$arraycontent = explode('##SEPERATOR##', $text);
dd($arraycontent);

Above code is working fine but it is not removing the <p></p> between the separator.

the response is like below,

array:4 [▼
  0 => "<p>Author Name, Department</p><p>"
  1 => "</p><p>Author Name, Department</p><p>"
  2 => "</p><p>Author Name, Department</p><p>"
  3 => "</p><p>Author Name, Department</p>"
]

Here at 0 index <p> tag is opening and at index 1 </p> tag is closing i want to strip this one too in explode. how to do this? so that response should be like below,

array:4 [▼
  0 => "<p>Author Name, Department</p>"
  1 => "<p>Author Name, Department</p>"
  2 => "<p>Author Name, Department</p>"
  3 => "<p>Author Name, Department</p>"
]
0 likes
8 replies
furqanDev's avatar
$arraycontent = explode('<p>##SEPERATOR##</p>', $text);
Dunsti's avatar
Dunsti
Best Answer
Level 6

Really?

Try this:

$arraycontent = explode('<p>##SEPERATOR##</p>', $text);
dd($arraycontent);
Deekshith's avatar

@Dunsti Thank you for the reply but the concern is what if it has different tag?

tykus's avatar

You could use a Stringable chain, eg.

Str::of($text)
    ->explode("\n") // array item for every new line character
    ->filter() // remove empty lines
    ->reject(fn ($line) => str_contains($line, '##SEPERATOR##')) // remove by SEPARATOR keyword
    ->values(); // re-key the array
Deekshith's avatar

@tykus this one adding extra \r like below,

#items: array:10 [▼
    0 => "<p>Author Name, Department</p>"
    1 => "\r"
    2 => "\r"
    3 => "<p>Author Name, Department</p>"
    4 => "\r"
    5 => "\r"
    6 => "<p>Author Name, Department</p>"
    7 => "\r"
    8 => "\r"
    9 => "<p>Author Name, Department</p>"
  ]
tykus's avatar

@Deekshith it is adding \r, that information is already there; just filter it out

Deekshith's avatar

@tykus i am not getting how to filter \r. it would be helpful if you send me the piece of code Thank you.

Please or to participate in this conversation.