Simplest way to parse a title from an HTML file using PHP functions only, no extra classes -
so far i've been trying simple way stract title html page.
this simple:
$url = "http://localhost";
use function extract title tag using php functions or regular expressions, not want use external classes such simple_html_dom or zend_dom... want simple way php only... can post sample code extract title tag localhost?
i've tried using domdocument() class, simple_xml_parse(), , none of them success
i tried this:
<?php $dom = new domdocument(); $dom->loadhtml('pag.html'); $items = $dom->getelementsbytagname('title'); foreach ($items $title) { echo "title"; }
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 title</title> </head> <body> <h1>hello</h1> </body> </html>
output:
this title
Comments
Post a Comment