Reputation:
So far I've been trying to get a simple way to stract a title from an HTML page.
This simple:
$url = "http://localhost";
Use any function to extract the title tag using only PHP functions or regular expressions, I do not want to use any external classes such as simple_html_dom or Zend_Dom... I want to do it the simple way with PHP only... can anyone post a sample code to simply extract the title tag from localhost?
I've tried using DOMdocument() class, simple_xml_parse(), and none of them with success
I tried like this:
<?php $dom = new DOMdocument();
$dom->loadhtml('pag.html');
$items = $dom->getElementsByTagName('title');
foreach ($items as $title) { echo "title"; }
Upvotes: 1
Views: 1381
Reputation: 7791
With DOM:
<?php
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents("1.html"));
$items = $doc->getElementsByTagName("title");
if($items->length > 0){
echo $items->item(0)->nodeValue;
}
?>
With Regular Expressions:
<?php
$html = file_get_contents('1.html');
preg_match("/<title>([^<]*)<\/title>/im", $html, $matches);
echo $matches[1];
?>
1.html
<html>
<head>
<title>This is the title</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
Output:
This is the title
Upvotes: 2