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.