What is the recommended method in PHP to extract the title from a webpage string?
To extract the title from a webpage string in PHP, you can use regular expressions to search for the <title> tag and extract the content within it. This can be achieved by using the preg_match function with a regex pattern that matches the title tag and captures the title content. Once the title content is extracted, you can then display or use it as needed in your application.
// Sample webpage string
$html = "<html><head><title>Example Page</title></head><body><h1>Hello World</h1></body></html>";
// Extract title from webpage string
if (preg_match("/<title>(.*?)<\/title>/i", $html, $matches)) {
$title = $matches[1];
echo "Title: " . $title;
} else {
echo "Title not found";
}