Hey guys,
i wish php had a getelementbyclass function in the dom. I have an html page that im parsing and there is an image with a class attribute that is a unique value. Can i get the src attribute of the img, by finding the image using the value of its class attribute? Thanks.
xml parsing trouble
Moderator: General Moderators
Re: xml parsing trouble
There is no getElementByClass in the DOM spec therefore there is no such function in DOMDocument either.
Use getElementsByTagName() and look for one with the right className.
Use getElementsByTagName() and look for one with the right className.
-
RustyDoorknobs
- Forum Newbie
- Posts: 15
- Joined: Mon Sep 07, 2009 4:50 pm
Re: xml parsing trouble
could you post some code? thanks
Re: xml parsing trouble
Thats the thing about Class names and ID's, Class names are not unique and ID's are.RustyDoorknobs wrote:Hey guys,
i wish php had a getelementbyclass function in the dom. I have an html page that im parsing and there is an image with a class attribute that is a unique value. Can i get the src attribute of the img, by finding the image using the value of its class attribute? Thanks.
Like tasairis said you could write a little function to find them if you have to, but if you can why not make them ID's.
Code: Select all
function find_elements_by_class($dom, $class_to_find)
{
$found = array();
$elements = $dom->getElementsByTagName('*');
foreach($elements as $element)
{
if($class_name = $element->getAttribute('class'))
{
// Class attributes can have more then one class
if(preg_match('/'.$class_to_find.'(?:\s|$)/i', $class_name))
{
$found[] = $element;
}
}
}
return (array)$found;
}-
RustyDoorknobs
- Forum Newbie
- Posts: 15
- Joined: Mon Sep 07, 2009 4:50 pm
Re: xml parsing trouble
heres the code i tried:
.... Heres what i got:
Code: Select all
<?php
//Load team profile page:
$u = $_GET["u"];
$dom = new DOMDocument();
$dom->load( $u );
//Load in team avatar:
function find_elements_by_class($dom, $class_to_find)
{
$found = array();
$elements = $dom->getElementsByTagName('*');
foreach($elements as $element)
{
if($class_name = $element->getAttribute('class'))
{
// Class attributes can have more then one class
if(preg_match('/'.$class_to_find.'(?:\s|$)/i', $class_name))
{
$found[] = $element;
}
}
}
return (array)$found;
}
$class_to_find = "icon64 icon-f-lt";
find_elements_by_class($dom, $class_to_find);
?>
.... Heres what i got:
Any ideas?Warning: DOMDocument::load() [domdocument.load]: Opening and ending tag mismatch: link line 13 and head in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 23 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Opening and ending tag mismatch: td line 217 and tr in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 221 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Opening and ending tag mismatch: tr line 207 and tbody in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 222 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Opening and ending tag mismatch: tbody line 205 and table in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 223 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Opening and ending tag mismatch: table line 186 and div in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 224 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Entity 'nbsp' not defined in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 270 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Entity 'nbsp' not defined in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 271 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Entity 'trade' not defined in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 275 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Attribute class redefined in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 284 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Entity 'nbsp' not defined in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 286 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Entity 'copy' not defined in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 304 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Opening and ending tag mismatch: div line 103 and body in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 335 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Opening and ending tag mismatch: body line 25 and html in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 336 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Premature end of data in tag head line 4 in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 336 in /home/myavatar/public_html/another.php on line 5
Warning: DOMDocument::load() [domdocument.load]: Premature end of data in tag html line 3 in http://gamebattles.com/xbox360/left-4-d ... zombangers, line: 336 in /home/myavatar/public_html/another.php on line 5
Re: xml parsing trouble
The website is invalid xHTML so the parser is throwing warning errors.
You can hide them with an @ symbol before the function call.
You can hide them with an @ symbol before the function call.
Code: Select all
// hide function errors
@$dom->load( $u );
// ...
// Function returns an array of all the elements with the class name
$elements = find_elements_by_class($dom, $class_to_find);