Cookie Consent by Free Privacy Policy Generator

Create an excerpt using the first paragraph <p> with PHP

The PHP snippet below scrapes the current URL to extract the first paragraph on the page. They can be useful for creating excerpts and creating dynamic meta descriptions.

PHP:
<?php
// load the URL
$url = https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
// Get current URL contents
$content = file_get_contents($url);

// load the content
$dom = new domdocument();
$dom->loadhtml($content);

// output first paragraph
$firstpara = $dom->getElementsByTagName('p');
echo " {$firstpara->item(0)->nodeValue}\n";

?>
 
Last edited:
Back
Top