How can PHP be used to extract specific content from a website, such as text between specific HTML tags like <h2> and </h2>?
To extract specific content from a website using PHP, you can utilize PHP's DOMDocument class to parse the HTML structure of the webpage and extract the desired content based on specific HTML tags. You can use methods like getElementById, getElementsByTagName, or XPath queries to target the specific HTML elements containing the content you want to extract.
// URL of the website to extract content from
$url = 'https://www.example.com';
// Create a new DOMDocument object
$doc = new DOMDocument();
// Load the HTML content from the URL
$doc->loadHTMLFile($url);
// Get all <h2> tags from the webpage
$h2Tags = $doc->getElementsByTagName('h2');
// Loop through each <h2> tag and extract the text content
foreach ($h2Tags as $tag) {
echo $tag->textContent . "<br>";
}