Reputation: 89
I'm using php to get a part of a html file:
HTML file:
<div class="titles">
<h2><a href="#">First Title</a></h2>
</div>
PHP file:
<?php
include_once('simple_html_dom.php');
$url = 'http://example.com';
$html = file_get_html($url);
$titles = $html->find('.titles');
$heading = $titles->find('h2')[0];
$link = $heading->find('a')[0];
echo $link;
//result: <a href="#">First Title</a>
?>
How can I separately get the value of href and 'a' tag?
Because I want to save the title and link into the database,
I need '#' and 'First Title' not the 'a' tag.
Upvotes: 4
Views: 107
Reputation: 422
U can use DOMDocument and DOMXpath object's (>=php5)
ref: http://php.net/manual/en/class.domdocument.php
part of sample code:
$html = '<div class="titles">
<h2><a href="#">First Title</a></h2>
</div>';
$page = new DOMDocument();
$page->loadHtml($html);
$xpath = new DOMXpath($page);
$a = $xpath->query("//a");
for ($i=0; $i < $a->length; $i++) {
$_a = $a->item($i);
echo $_a->getAttribute("href");
echo "<br>";
echo $_a->textContent;
}
Upvotes: 0
Reputation: 522015
$link
should be a Simple HTML Element object, of which you can access attributes using $link->href
and the text contents as $link->plaintext
. See http://simplehtmldom.sourceforge.net/manual.htm.
Upvotes: 6