Here is a ordinary HTML file, named 1.html
<html>
<head>
<meta http-equiv="content-type" content="text/html"/>
<title> try document</title>
</head>
<body>
<h2> a h2 line</h2>
<p> a p line</p>
<form action="" >
<select>
<option> white</option>
<option> blue</option>
<option>red</option>
</select>
</form>
</body>
</html>
I try to create a php file to parse the HTML file, like this:
<?php
$doc= new DomDocument(1.0);
$doc->loadHtmlFile('1.html');
$head=$doc->getElementsByTagName('head');
gettitle($head);
function gettitle($head){
foreach($head as $header){
echo ($header->textContent);
}
}
?>
This will output "try document" which is the text of title, but this result is out of my expect.
$head=$doc->getElementsByTagName('head'); at this stage, $head should be a nodelist of head, and of course there is only one <head> element in the file. so I think $head is like an array which has only one element which is the <head> element.
the we call function gettitle.
In the function, we execute foreach($head as $header){echo ($header->textContent);} for each iteration, $header should be a <head> element, but the result is, it output its subelement <title>'s text. Is there any wrong? or $head refers to an array of all the son elements of <head>?
thanks
using DOM to parse a HTML , Help please!!
Moderator: General Moderators