English 中文(简体)
PHP DOMDocument GetElementsByTagName Not Finding Elements
原标题:

I have an HTML page containing alot of meta tags and I want to parse them to find certain ones. Here is the code I am using, but it s not picking up any of the tags.

$dom = new DOMDocument();  
$dom->preserveWhiteSpace = false;  
$dom->loadHtml($contents);  
$metaChildren = $dom->getElementsByTagName( meta );  
var_dump($metaChildren);

Here is a snippet of the HTML I am using (I replaced the arrow with a brace):

[meta name="GZPlatform" content=" pc"]  
[meta name="GZFeatured" content=" Gone Gold"]  
[meta name="GZHeadline" content=" pc"]  
[meta name="GZP_ID" content=" pc 21153"]  

Any Ideas?

最佳回答

Are you sure the tags aren t being matched? What is the output of var_dump? What value do you get when you use var_dump($metaChildren->length)? Your code seems to work here:

<?
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHtmlFile( test.html );
$metaChildren = $dom->getElementsByTagName( meta );
for ($i = 0; $i < $metaChildren->length; $i++) {
  $el = $metaChildren->item($i);
  print $el->getAttribute( name ) .  =  . $el->getAttribute( content ) . "
";
}
?>

Gives output:

GZPlatform= pc
GZFeatured= Gone Gold
GZHeadline= pc
GZP_ID= pc 21153
问题回答

My guess would be that the HTML is not valid and that the $dom->loadHtml call is failing. I believe that call returns true|false. So maybe something like this:

if($dom->loadHtml($contents)){
    $metaChildren = $dom->getElementsByTagName( meta );
}else{
    //handle properly
}

COuld it be that the parser expects you to close the meta tags?

<meta name="name" />

or

<meta name="name"></meta>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签