Regex for splitting a string with some conditions in PHP

this is my problem, I'm not very good with regular expressions, I need split a string in this way, for example I have this string:

1002;string1<br>
string2<br>
string3
1003;string1<br>
string2<br>
string3

So I need to split the whole string in this way: separating each item block, in the above case: one of them would be:

1003;string1<br>
string2<br>
string3

I've tried to split by $data = explode("\n", $string); but it separates each line, and I want to ignore if the \n is preceded by <br>, I suppose I have to use regular expressions, maybe using the preg_split() function?

Thanks in advance

Upvotes: 1

Views: 57

Answers (1)

anubhava
anubhava

Reputation: 784998

You can use:

$matches = preg_split('/(?<!<br>)\n/', $input);

RegEx Demo

This will split the input when <br> is not there before \n.

Upvotes: 2

Related Questions