What are some common methods for parsing and extracting information from HTML using PHP?

When parsing and extracting information from HTML using PHP, one common method is to use the DOMDocument class to load the HTML content and then navigate through the DOM tree to find and extract the desired information using methods like getElementById, getElementsByTagName, or querySelector. Another approach is to use regular expressions to match and extract specific patterns or elements from the HTML content. Additionally, PHP libraries like Simple HTML DOM Parser can be used to simplify the process of parsing and extracting information from HTML.

// Method 1: Using DOMDocument
$html = '<html><body><h1>Hello, World!</h1></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$heading = $dom->getElementsByTagName('h1')[0]->nodeValue;
echo $heading; // Output: Hello, World!

// Method 2: Using regular expressions
$html = '<h1>Hello, World!</h1>';
preg_match('/<h1>(.*?)<\/h1>/', $html, $matches);
$heading = $matches[1];
echo $heading; // Output: Hello, World!

// Method 3: Using Simple HTML DOM Parser
include('simple_html_dom.php');
$html = file_get_html('http://www.example.com');
$heading = $html->find('h1', 0)->plaintext;
echo $heading; // Output: Hello, World!