将字符串与[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.
我有以下示例字符串: p>
$ string = '祝你快乐&lt; span style =“font-size:14px;”&gt;圣诞节&lt; / span&gt; 和一个快乐的新&lt; span style =“font-size:18px;”&gt; year&lt; / span&gt;!'
code> pre>
现在我试图爆炸这个 字符串,以便输出: p>
$ arr [0] ='我希望你快乐'
$ arr [1] ='&lt; span style =“字体 -size:14px;“&gt;圣诞节&lt; / span&gt;'
$ arr [2] ='和一个快乐的新'
$ arr [3] ='&lt; span style =”font-size:18px;“ &gt;年&lt; / span&gt;'
$ arr [4] ='!'
code> pre>
我尝试了 p>
$ arr = explode('&lt; span style =“font-size:',$ string);
code> pre>
但当然我包括了 整个字符串直到下一个开始&lt; span&gt; code> -Tag。 p>
我还尝试将 preg_match_all code>与foreach循环一起使用 使用font-sizes: p>
preg_match_all('〜\&lt; span style =“font-size:'。$ fontSize。'px;”\&gt;(。*? )\&lt; \ / span \&gt;〜',$ string,$ output [$ fontSize]);
code> pre>
但是其他不在之间的字符串 标签不包括在内。但我在描述中需要它们 ed order。 p>
如何将其正确转换为给定的数组? 我需要它用于PHP lib PDFlib,它无法读取HTML。 p>
div>
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);