剥离HTML字符串但不丢失换行符

I have a string which contains a lot of HTML (HTML email body).

What I want to do is lose all the HTML but keep the line breaks, I tried strip_tags() but this didn't work out for me, it makes the string just 1 line.

After this has been done, I want to echo only the first 5 lines from this string.

Anyone got an idea?

Update:

$string = strip_tags($string, '<p><br><br />');

Seems to be working, now I have to select the first 5 lines from the string, anyone an idea?

Update: Array keeps giving a few empty values. I managed to remove the empty ones but the array doesn't restart the counting. Array looks like this now:

Array ( [8] =>

Dit is een nieuwe test met meerdere regels.
[9] =>

Dit is regel nummer 2.
[10] =>

Dit is regel nummer 3.

But it should start counting at 0 again.

array_filter() array_merge() etc. do not work.

Fixed it now, didn't use the email message anymore but get the message data from the DB, now I can strip the string and explode, starts counting at 0 now!

Thanks for the thoughts.

Use strip_tags with a second parameter, like this: strip_tags($html, '<p><br>'). As you can see, the second parameter takes in $allowable_tags as in the docs ..

See this question/answer also.

Perhaps the easiest to get the first few lines then is using explode:

$lines = explode("<br>", $string); // take first 5 from this array by exploding new lines 

Get the lines of course using $lines[0] (1st line), $lines[1] (2nd line) etc.