Jump to content
php.lv forumi

Atrast URLi Dokumentaa ar DOM paliidziibu


Bunkertor 7

Recommended Posts

Sveiki,

 

Meeginu uzskriptot skriptu, kas paraadiitu, vai attieciigaa HTML dokumentaa atrodas links uz kaadu konkreetu resursu. Piemeeram, skripts aiziet uz vilks.lv un paarbauda, vai kaut kur kodaa ir links uz lacis.lv

Es saakumaa domaaju, ka to var izdariit ar $xpath->query paliidziibu, bet jo vairaak manuaalus lasu, jo paarliecinos, ka laikam nevar.

 

Ideja bija kaut kaa taa:

 

<?php
$doc = DOMDocument::loadHTMLFile('http://www.whatever');

$xpath = new DOMXPath($doc);
$links = $xpath->query('//a/@href/MyKeyWordInTheURL');
#$urlz = array();
foreach ($links as $link)
{

#    $urlz[] = $link->value;
echo $link->value . "\n";
echo "Linka Teksts:" . $link->nodeValue . "\n";
}

?>

 

Varbuut labaak meeginaat ar Preg Match?

Link to comment
Share on other sites

if (strpos(strip_tags(file_get_contents('http://vilks.lv'),'<a>'), 'href="http://lacis.lv') !== false) {
/// oh yeahhhhh
}

 

Pret to display:none tāpat neko neizdarīs. To jau var ielikt cssā.

Edited by indoom
Link to comment
Share on other sites

Piemērs ar simplexml. Noteikti var izdarīt arī ar DOM query, bet negribas tagad testēt.

 

<?php

$doc = DOMDocument::loadHTMLFile('http://www.whatever');

$xpath = new DOMXPath($doc);
$xml = simplexml_import_dom($doc);
$links = $xml->xpath("//a[@href='http://vilks.lv/']");
var_dump($links);
foreach($links as $link){
print $link['href']."\n";
}

 

Es gan to href apstrādātu ar PHP, t.i., $xml->xpath("//a"); un tad tajā ciklā parsētu href. Mazums tur kaut-kādi www. vai path galā... XPath, protams, ir arī "contains" - check w3schools.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...