0

I can not find the right parsing solution from another link. I want to parse content from part of site.

I need get string "Director".

<div>
    <div class="jobtitle">Director</div>
</div>
 

This is code display all content from link

$url = 'somelink.com';
$result = file_get_contents($url);

echo($result);
8
  • 2
    Could you be more specific please ? Commented Jun 24, 2020 at 18:54
  • Show us your parse() function and tell us what does not work as expected - actual vs. expected outcome. And let us know in detail what you've tried to debug your code. Please read How to Ask Commented Jun 24, 2020 at 18:54
  • Do you want to retrieve content of elements that has a certain class? Is that what you are saying? Commented Jun 24, 2020 at 18:59
  • @bestprogrammerintheworld yes absolutely Commented Jun 24, 2020 at 19:04
  • 1
    @bestprogrammerintheworld thanks for library, best solution for my task. Commented Jun 25, 2020 at 21:32

1 Answer 1

1

Try :

$url = 'somelink.com';
$result = file_get_contents($url);

$dom = new DOMDocument;
$dom->loadHTML($result);

$classname="jobtitle";
$finder = new DomXPath($dom);
$spaner = $finder->query("//*[contains(@class, '$classname')]");

Or if you don't want to use XPath traversal, you can simply loop throught all the div nodes and get the class attribute that is equal to "jobtitle".

This is the code:

$url = 'somelink.com';
$result = file_get_contents($url);

$dom = new DOMDocument;
$dom->loadHTML($result);

$nodes = $dom->getElementsByTagName ("div");

$wanted_node ;

foreach( $nodes $n) {
    if ($n->getAttribute('class') == "jobtitle"){
        $wanted_node = $n;
    }
}

//If wanted_node is not null(the node with class=jobtitle is found
if (isset ($wanted_node)){
    echo $wanted_node;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.