James Taylor James Taylor - 2 months ago 5x
PHP Question

Build Stripped HTML Array from String in PHP

I have a String which looks something like this:

$html_string = "<p>Some content</p><p>separated by</p><p>paragraphs</p>"

I'd like to do some parsing on the content inside the tags, so I think that creating an array from this would be easiest. Currently I'm using a series of
to achieve what I want:

$stripped = explode('<p>', $html_string);
$joined = implode(' ', $stripped);
$parsed = explode('</p>', $joined);

which in effect gives:

array('Some content', 'separated by', 'paragraphs');

Is there a better, more robust way to create an array from HTML tags? Looking at the docs, I didn't see any mention of parsing via a regular expression.

Thanks for your help!


If its only that simple with no/not much other tags inside the content you can simply use regex for that:

$string = '<p>Some content</p><p>separated by</p><p>paragraphs</p>';

preg_match_all('/<p>([^<]*?)<\/p>/mi', $string, $matches);


which creates this output:

array(3) {
  string(12) "Some content"
  string(12) "separated by"
  string(10) "paragraphs"

Keep in mind that this is not the most effective way nor is it the fastest, but its shorter then using DOMDocument or anything like that.