`strip_tags` not working with pretty simple string in PHP

I have a long HTML code that contains <script>, <style>, <div>… tags and strip_tags usually works fine. But with the string below, it’s buggy:

$string = '

    <div>BEGINNING</div>

    <script>
    for (let i=0;i<resultado4448.length;i++) {

    }
    </script>

    <div>END</div>

';

echo strip_tags($string);

The code above outputs:

BEGINNING


for (let i=0;i

Which is incorrect, because the END div was not displayed. I think strip_tags got confused with <resultado4448 believing it is a tag and since it didnt find the closing one, it kept searching till the end of the string.

So, if strip_tags is not reliable for this case, is there any other native function or thing that I can do? I dont want to parse the entire document just to get the tags stripped, I have to parse millions of HTML strings each day and strip_tags is being very good and has a good performance, but if I had to parse all HTML codes, it would take forever.