2

I want the value of the rel attribute of the anchor tag associated with the search domain.

I have to change the domain "blog.zeit.de/berlinjournal" instead of "http://blog.zeit.de/berlinjournal/". Use this domain and find out rel Val

@Sam Onela, code not working for this domain. Please help me to solve this error.

My code is:

$domain = 'blog.zeit.de/berlinjournal';
$handle = fopen($domain, 'r');
$content = stream_get_contents($handle);
fclose($handle);
if ((strpos($content, $domain) !== false)) {
        echo 'true'; // true if $domain found in view source content
} 

Get the clear idea in blow image

enter image description here

Puja
  • 451
  • 2
  • 5
  • 20

1 Answers1

1

Create an instance of DOMDocument, call the loadHTML() method, then use simplexml_import_dom() to get an instance of a SimpleXMLElement, on which the xpath() method can be used to query for that anchor tag.

You may also notice warnings printed to the screen when loading the html. To set it to use the internal error handler, use libxml_use_internal_errors(true); - thanks to @dewsworld for this answer.

libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($content);
$xml = simplexml_import_dom($doc);
$results = $xml->xpath("//a[@href='$domain']");
if (sizeof($results)) {
    echo 'rel: '.$results[0]['rel'].'<br>';
}

See it demonstrated in this phpfiddle.

Update

Since the HTML of the original URL has changed and the requirement is now to find the rel attribute of a different anchor tag, that can be done with the contains() xpath function.

$searchDomain = 'rballutschinski.wordpress.com/';
if ((strpos($content, $searchDomain) !== false)) {
    $doc = new DOMDocument();
    $doc->loadHTML($content);
    $xml = simplexml_import_dom($doc);
    $results = $xml->xpath("//a[contains(@href,'$searchDomain')]");
    if (sizeof($results)) {
        $rel = $results[0]['rel'];
    }

See a demonstration in this phpfiddle.

Community
  • 1
  • 1
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • Thanks @Sam Onela – Puja Apr 11 '17 at 11:54
  • your code is working well bt when I m using my $searchdomin = https://www.paintyourlife.com/watercolor-portraits.php and $domain = http://www.messhall.org/contemporary-watercolour-portrait-artists-who-have-changed-the-face-of-portraits/ then it will give me an error.. I put my code in https://pastebin.com/j4E7nAYQ plz help me to solve this error.. Thank you – Puja Jun 01 '17 at 05:33
  • if you are seeing another error that you are not familiar with, and don't find any documentation on it, then perhaps [ask another question](https://stackoverflow.com/questions/ask). – Sᴀᴍ Onᴇᴌᴀ Jun 01 '17 at 15:14