My Website - Home
$html = 'My Website - Home '; $doc = new DOMDocument(); $doc->loadHTML($html); $title = $doc->getElementsByTagName('title')->item(0)->textContent; echo $title; // Output: My Website - Home
function getTitleFromHtml($html) { $doc = new DOMDocument(); @$doc->loadHTML($html); $title = $doc->getElementsByTagName('title')->item(0)->textContent; return $title; } $html = file_get_contents('http://example.com'); $title = getTitleFromHtml($html); echo $title; // Output: Example DomainHere, we define a function getTitleFromHtml that expects an HTML string as a parameter. We then load this HTML content into a DOMDocument object and extract the Title content as before. Finally, we return the title text. Package library: This example also uses the core PHP DOMDocument class, which is built-in and requires no external package.