How can i get the links into an html list with php?
I have the following html code in HTML:
<ul><li><a href="/webapps/portal/frameset.jsp?tab_tab_group_id=noActiveTabGroup&url=%2Fwebapps%2Fblackboard%2Fexecute%2Flauncher%3Ftype%3DCourse%26id%3D_343744_1%26url%3D" target="_top">CCM.TC2012.1.1111.20132 : Proyecto de desarrollo de software ( 11 Gpo 1)</a></li> <li><a href="/webapps/portal/frameset.jsp?tab_tab_group_id=noActiveTabGroup&url=%2Fwebapps%2Fblackboard%2Fexecute%2Flauncher%3Ftype%3DCourse%26id%3D_343703_1%26url%3D" target="_top">CCM.TC3003.1.1111.20088 : Diseño y arquitectura de software ( 11 Gpo 1)</a></li> <li><a href="/webapps/portal/frameset.jsp?tab_tab_group_id=noActiveTabGroup&url=%2Fwebapps%2Fblackboard%2Fexecute%2Flauncher%3Ftype%3DCourse%26id%3D_343700_1%26url%3D" target="_top">CCM.TC3007.1.1111.20085 : Proyecto integrador de tecnologías computacionales ( 11 Gpo 1)</a></li> <li><a href="/webapps/portal/frameset.jsp开发者_Python百科?tab_tab_group_id=noActiveTabGroup&url=%2Fwebapps%2Fblackboard%2Fexecute%2Flauncher%3Ftype%3DCourse%26id%3D_374651_1%26url%3D" target="_top">CCM.WA3001.12.1111.20338 : Competencias profesionales ( 11 Gpo 12)</a></li></ul>
And I'm trying to get the links of the lists (the href value) with the next code:
$dom = new domDocument;
@$dom->loadHTML($materias);
$dom->preserveWhiteSpace = false;
$ul = $dom->getElementsByTagName('ul');
$value = $dom->getElementsByTagName("ul")->item(0)->getAttributeNode("href");
$lis = $ul->item(0)->getElementsByTagName('li');
foreach ($lis as $li){
echo li->nodeValue;
}
But I only get the text and not the value of the href. Could you help me, please?
The href is an attribute of the a element. You are fetching UL and LI, so there is no href attribute to fetch.
Try with this XPath:
/html/body/ul/li/a/@href
This will fetch all the href attribute nodes
$li->getAttribute( 'href' );
?
Edit: Err - as i looked into manual it looks like $li->attributes->getNamedItem("href")->nodeValue;
精彩评论