문제

Could someone please tell me why this ain't working.

I'm trying to get a certain href and from a page by using php dom and that href - www.imdb.com/title/tt-some-id contains the word imdb so in the example below i try to get the href by using php function strpos to look for the word imdb but it don't seen to work.

$page = 'www.someurl.com';
$data = array();
$dom = new DOMDocument();
@$dom->loadHTML($page);

$data['imdb_link'];

$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
    $href = $link->getAttribute('href');
    if (false !== strpos($href,'imdb')) {
        $data['imdb_link'] = $href;
    } else {
        $data['imdb_link'] = '';
    }
}

And the links from the page

<a href="some-url.com"></a>
<a href="www.imdb.com/title/some-id"></a>
<a href="another-url.com"></a>
<a href="another-url.com"></a>

Could someone please tell me why it ain't working thanks

도움이 되었습니까?

해결책 2

That is actually working , but you are overwriting it..

As you can see your final <a> href does not contain the text imdb , so that will be overwritten by your previously found result by your else statement.

Well how to fix it ?

Just remove the else part from your code.

Working Demo

다른 팁

You could move the logic to Xpath, too. The result will be an empty string of no matching element is found:

$page='<a href="some-url.com"></a>
<a href="www.imdb.com/title/some-id"></a>
<a href="another-url.com"></a>
<a href="another-url.com"></a>';
$dom = new DOMDocument();
@$dom->loadHTML($page);
$xpath = new DOMXpath($dom);

$data['imdb_link'] = $xpath->evaluate(
  'string(//a[contains(@href, "imdb")]/@href)'
);
var_dump($data);

Output: https://eval.in/149602

array(1) {
  ["imdb_link"]=>
  string(26) "www.imdb.com/title/some-id"
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top