8

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

4 Answers 4

14

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>
)
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic solution.
4

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

2 Comments

what in case of nested paragraphs (if any) ?
@Letmesee: It certainly doesn't work fine, but why do you want nested paragraphs?
2

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

1 Comment

@pbaldauf Have you try my answer,Here it is forking fine for me
0

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

1 Comment

Does it give cases where to closing tag isn't </p>?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.