使用html内容动态填充数组而不剥离标记

I have some HTML Content.

Eg.

<p>Ask a question</p><p>Wait for an answer</p><p>Vote up an Answer</p>

I want to use php to input each paragraph/div or any other html element separately as elements of an array

$arr[0]="<p>Ask a question</p>";
$arr[1]="<p>Wait for an answer</p>";

I want to do the above task dynamically.

Lots of ways to do this. My first approach would be to use preg_match_all():

Assume your html is in $html:

preg_match_all( '|<p>.+?</p>|si', $html, $matches );

$matches will then be an array-of-arrays of 1 element x N elements, where N is the number of matches.

$matches[0][0] == '<p>Ask a question</p>';
$matches[0][1] == '<p>Wait for an answer</p>';
...

Edit: this can be generalized to match other tags, but a DOM parser should be used instead after the complexity requirements outweigh what regular expressions are capable of parsing.

If you want to match a fixed set of non-nestable tags, this approach will work, but if the desired tags are nestable or self-closing, then regular expressions are not the way to go and using a simple DOM parser will be the right solution.