, / XPath, -, , . - :
i = 0
while (//img[i][@src])
if (//img[i][@alt])
return alt
else if (//img[i][@description])
return description
else if (//img[i]/../p[0])
return p
else
return (//title)
i++
XPath ( ):
function ph_DOM($html, $xpath = null)
{
if (is_object($html) === true)
{
if (isset($xpath) === true)
{
$html = $html->xpath($xpath);
}
return $html;
}
else if (is_string($html) === true)
{
$dom = new DOMDocument();
if (libxml_use_internal_errors(true) === true)
{
libxml_clear_errors();
}
if ($dom->loadHTML(ph()->Text->Unicode->mb_html_entities($html)) === true)
{
return ph_DOM(simplexml_import_dom($dom), $xpath);
}
}
return false;
}
:
$html = file_get_contents('http://en.wikipedia.org/wiki/Photography');
print_r(ph_DOM($html, '//img'));
print_r(ph_DOM($html, '//img[@src]'));
print_r(ph_DOM($html, '//img[@src]/..'));
print_r(ph_DOM($html, '//img[@src]/../..'));
print_r(ph_DOM($html, '//title'));