E
E
Evgeny Orlov2018-10-02 09:34:54
PHP
Evgeny Orlov, 2018-10-02 09:34:54

Php, DiDom parser. How to display href?

Good, I began to study this "wonderful" parser and faced the fact that the cat cried on the Internet of examples.
I'm looking for help from a guru on this parser.
Actually what is
Parsim a certain site. We receive the list of all div, class=links And how now in found to receive all links? I do:
$document = new Document('некий сайт', true);
$r_div=$document->find('div[class="links"]');

foreach($r_div as $div)
{
  $track=$div->find('a[class="track-more-info"]');
}

But when I try to get the href value, the question arose, how to do it.
There is only one example on the wiki.
echo $document->html();
Well, ok, we do it for verification.
echo $track->html();
And we get
Fatal error: Call to a member function html() on a non-object

Although var_dump says that there is information in $track
I tried $track->href and other options...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick Sdk, 2018-10-02
@Miracl

based on the "documentation" Imangazaliev / DiDOM
should work like this:
or

$track->attr('href');
$track->getAttribute('href');

so even in Russian there is documentation
which wiki did you read?
foreach($r_div as $div)
{
  $track=$div->find('a[class="track-more-info"]');
}

here in the $trackresult is an array with the found elements, a[class="track-more-info"]
so you should output either like this:
foreach ($track as $item) {
    echo $item->href;
}

or
$list = $div->find('a[class="track-more-info"]');
$track = array_shift($list);
if (!empty($track)) {
    echo $track->href;
}

T
tigra, 2018-10-02
@tigroid3

$track = $div->find('a[class="track-more-info"]');
$track->getAttribute('href');

or try like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question