Dylan
Dylan

Reputation: 33

extract content between all paragraph tags

How do I extract only the content between all of the <p></p> tags in a given string? I know preg_match or regex but I spent hours already trying to put this stuff together. thought I'd just ask. simple question and a simple answer i hope. Thanks in advance. this would be in PHP, btw.

Upvotes: 1

Views: 4009

Answers (1)

ig0774
ig0774

Reputation: 41287

DOMDocument::loadHTML. Maybe not the fastest option, but should be simple.

Something like (it's been a while since I've actually written PHP...):

$doc = new DOMDocument();
$doc->loadHTML($string);
foreach($doc->getElementsByTagName('p') as $paragraph) {
    // do something with $paragraph->textContent
} 

Upvotes: 7

Related Questions