How I get the value of "rel" attribute from the html view source using php?
Get the solution ↓↓↓Solution:
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, uselibxml_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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your system folder path does not appear to be set correctly. please open the following file and correct this: index.php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.