到目前为止,我一直试图从HTML页面中找到一种简单的方法来构建一个标题。
这很简单:
$url = "http://localhost";
使用任何函数提取标题标签,只使用PHP函数或正则表达式,我不想使用任何外部类,如simple_html_dom或Zend_Dom.我只想用PHP简单的方式.有人可以发布一个示例代码来简单地从本地主机中提取标题标记吗?
我尝试过使用DOMdocument()类,simple_xml_parse(),但是没有一个成功。
我试过这样做:
<?php $dom = new DOMdocument();
$dom->loadhtml('pag.html');
$items = $dom->getElementsByTagName('title');
foreach ($items as $title) { echo "title"; }发布于 2015-04-26 01:29:21
使用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;
}
?>具有正则表达式:
<?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>输出:
This is the titlehttps://stackoverflow.com/questions/29872403
复制相似问题