179 shaares
1 result
tagged
dom
get content from links - for shaarli
// use DOMXPath
function file_get_contents_utf8($url) {
$content = file_get_contents($url);
return mb_convert_encoding($content, 'UTF-8',
mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}
//if (empty($charset)) echo "EMPTY".'', PHP_EOL;
//$dom->loadHTMLFile($url, LIBXML_NOWARNING | LIBXML_NOERROR);
$url="http://code.ambau.ovh/snippets/bash_network.html";
$dom = new DOMDocument();
$html = file_get_contents_utf8($url);
$dom->loadHTML($html, LIBXML_NOWARNING | LIBXML_NOERROR);
$xpath = new DOMXPath($dom);
$html_data = array('title','tags','code');
// title
foreach($xpath->evaluate("//title") as $node) {
$html_data['title'] = $node->nodeValue;
}
// tags
foreach($xpath->evaluate("//body['class']") as $node) {
$html_data['tags'] = $node->getAttribute('class');
}
// description
foreach($xpath->evaluate("//code") as $node) {
$html_data['code'][] = $node->nodeValue;
}
// print
echo "".$html_data['title'].'', PHP_EOL;
echo "tags=".$html_data['tags'].'', PHP_EOL;
foreach($html_data['code'] as $code) echo ''.$code.'', PHP_EOL;
// use DOMDocument
$title = $dom->getElementsByTagName('title')->item(0);
if (!is_null($title)) echo ''.$title->nodeValue.'', PHP_EOL;
# tags
$node = $dom->getElementsByTagName('body')->item(0);
if ($node->hasAttribute('class')) {
$tags = $node->getAttribute('class');
echo "tags=$tags".'', PHP_EOL;
} else echo "no class attribute";
$code = ;
foreach ($dom->getElementsByTagName('code') as $node) { echo ''.$node->nodeValue.'', PHP_EOL; }