将字符串与[PHP]之间的标签一起分解

I have the following sample string:

$string = 'I wish you a merry <span style="font-size: 14px;">Christmas</span> and a happy new <span style="font-size: 18px;">year</span>!'

Now I am trying to explode this string so that the output is:

$arr[0] = 'I wish you a merry '
$arr[1] = '<span style="font-size: 14px;">Christmas</span>'
$arr[2] = ' and a happy new '
$arr[3] = '<span style="font-size: 18px;">year</span>'
$arr[4] = '!'

I tried it with

$arr = explode('<span style="font-size: ', $string);

But of course then I include the whole string until the next opening <span>-Tag.

I also tried to use preg_match_all with a foreach loop over all used font-sizes:

preg_match_all('~\<span style="font-size:' . $fontSize . 'px;"\>(.*?)\<\/span\>~', $string, $output[$fontSize]);

But then the other strings which are not between the tags are not included. But I need them in the described order.

How can I convert that correctly to the given array? I need it for the PHP lib PDFlib which is not able to read HTML.

This is an easy and readable solution (not the prettiest):

$string = 'I wish you a merry <span style="font-size: 14px;">Christmas</span> and a happy new <span style="font-size: 18px;">year</span>!';

$string = str_replace("<span", "|<span", $string);
$string = str_replace("</span>", "</span>|", $string);

The string will end up like this:

'I wish you a merry |<span style="font-size: 14px;">Christmas</span>| and a happy new |<span style="font-size: 18px;">year</span>|!'

Now you can explode the string on "|":

$arr = explode("|", $string);

You should use the tools for the job. Here's a way to do this using DOMDocument (with a little trick).

$dom = new \DOMDocument();

$string = 'I wish you a merry <span style="font-size: 14px;">Christmas</span> and a happy new <span style="font-size: 18px;">year</span>!';
$dom->loadHTML("<div id='".($id=uniqid())."'>$string</div>"); //Trick, wrap in a div with a unique id.


foreach ($dom->getElementById($id)->childNodes as $child) { 
    echo $dom->saveHTML($child).PHP_EOL;
}

Outputs:

 I wish you a merry     
 <span style="font-size: 14px;">Christmas</span>    
  and a happy new     
 <span style="font-size: 18px;">year</span>    
 !

Of course instead of echo $dom->saveHTML($child) you can just put the results in an array e.g. $array[] = $dom->saveHTML($child);

Live example