How to extract all paragraphs text from URL in PHP

1 Answer

0 votes
$options = array(
	'http' => array('method' => "GET", 'header' => "User-Agent: Bot\n")
);
$context = stream_context_create($options);
$doc = new DomDocument();
$url = "https://www.website.com";
$doc->loadHTML(file_get_contents($url, false, $context));

$paragraphArray = $doc->getElementsByTagName("p");

foreach ($paragraphArray as $pa) {
		echo $pa->nodeValue . "<br><br>";
}



/*
run:

... text from $paragraphArray->item(0)->nodeValue

... text from $paragraphArray->item(1)->nodeValue

... text from $paragraphArray->item(...N)->nodeValue

*/

 



answered Sep 26, 2020 by avibootz
edited Oct 4, 2020 by avibootz

Related questions

1 answer 196 views
1 answer 127 views
1 answer 115 views
115 views asked Jul 16, 2024 by avibootz
2 answers 273 views
2 answers 165 views
165 views asked Aug 14, 2020 by avibootz
2 answers 184 views
...