Home » Php » html – PHP split each paragraph into array

html – PHP split each paragraph into array

Posted by: admin July 12, 2020 Leave a comment

Questions:

I want to split each paragraph into an array.

My current approach doesn’t work:

$paragraphs = preg_split( '|</p>|', $text, PREG_SPLIT_OFFSET_CAPTURE );

How can I get from this:

$text = <<<TEXT
        <p>Hello!</p>
        <p style="border: 1px solid black;">How are you,<br /> today?</p>
TEXT;

to this

$paragraphs = array(
     '<p>Hello!</p>',
     '<p style="border: 1px solid black;">How are you,<br /> today?</p>'
);
How to&Answers:

You can use DOMDocument() for this like as follows

 <?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);
?>

Output

Array
(
  [0] => <p>Hello!</p>
  [1] => <p style="border: 1px solid black;">How are you,<br> today?</p>
)

Answer:

You’ve forgotten the attribut limit and the flag is PREG_SPLIT_DELIM_CAPTURE

$text = <<<TEXT
        <p>Hello!</p>
        <p style="border: 1px solid black;">How are you,<br /> today?</p>
TEXT;
$paragraphs = preg_split( '|(?<=</p>)\s+(?=<p)|', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
//                                                here __^^


print_r($paragraphs);

Output:

Array
(
    [0] =>         <p>Hello!</p>
    [1] => <p style="border: 1px solid black;">How are you,<br /> today?</p>
)

Answer:

Their could be many ways..
You follow below steps as well.

$array = explode("</p>", $text);

This will break your text at every </p> into an array row
Then apply following for loop to add </p>

foreach($array as $row)
{ 
  $paragraphs[] = $row."</p>";
}

print_r($paragraphs);

Answer:

If you are sure every closing tag will be exactly

you can use explode:

  $paragraphs = explode('</p>', $text);

Else, if there may be any space you have to use a regex:

  $paragraphs = preg_split('/<\/\s*p\s*>/', $text);