James Taylor James Taylor - 3 months ago 8
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
explode
and
implode
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!

Answer

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

var_dump($matches[1]);

which creates this output:

array(3) {
  [0]=>
  string(12) "Some content"
  [1]=>
  string(12) "separated by"
  [2]=>
  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.