我想将每个段落拆分成一个数组.
我目前的做法不起作用:
$paragraphs = preg_split( '|</p>|',$text,PREG_SPLIT_OFFSET_CAPTURE );
我怎么能得到这个:
$text = <<<TEXT <p>Hello!</p> <p style="border: 1px solid black;">How are you,<br /> today?</p> TEXT;
对此
$paragraphs = array( '<p>Hello!</p>','<p style="border: 1px solid black;">How are you,<br /> today?</p>' );
您可以使用DOMDocument(),如下所示
原文链接:https://www.f2er.com/php/135862.html<?PHP $text = <<<TEXT <p>Hello!</p> <p style="border: 1px solid black;">How are you,<br /> today?</p> TEXT; $dom = new DOMDocument(); $paragraphs = array(); $dom->loadHTML($text); foreach($dom->getElementsByTagName('p') as $node) { $paragraphs[] = $dom->saveHTML($node); } print_r($paragraphs); ?>
产量
Array ( [0] => <p>Hello!</p> [1] => <p style="border: 1px solid black;">How are you,<br> today?</p> )