I must be overcomplicating this, but I can't figure it out for the life of me.
I have a standard html document stored as a string, and I need to get the contents of the paragraph. I'll make an example case.
$stringHTML=
"<html>
<head>
<title>Title</title>
</head>
<body>
<p>This is the first paragraph</p>
<p>This is the second</p>
<p>This is the third</p>
<p>And fourth</p>
</body>
</html>";
If I use
$regex='~(<p>)(.*)(</p>)~i';
preg_match_all($regex, $stringHTML, $newVariable);
I won't get 4 results. Rather, I'll get 10. I get 10 because the regex matches the first <p>
and first </p>
as well as the first <p>
and fourth </p>
How can I search between two words, and return only the results of whats between each paragraph?