Search code examples
phphtmlcurlsimple-html-dom

Using Simple Html Dom to extract bold text from a div


Working on a script project, literally spent the last 4 hours on this researching everything I can - my head literally doesn't function anymore on this matter and really need your help.

So I have a PHP cURL script that grabs data from a website. I can grab div's that have ID's and all that. But how can I grab specific text from within a DIV that does not have any ID/class/or anything specific other than the fact that its the only bold item in the div?

Here is the HTML Text on the website:

<div class="firststyle"><label for="calculator" class="class-coll-1">
                <p class="sr-only">Welcome to the calculator:</p> <b>What is one plus two?</b> </label></div>

What I am trying to parse/extract from this HTML part is JUST the text "What is one plus two?". How can define this specific part to be selected?

The only thing I can currently do is parse the entire div with the following script:

$html = str_get_html($response);
$the_question = $html->find('div[class=firststyle]');

However this gets all the text including the "Welcome to the calculator" label thing which I don't need.

Would it be possible to maybe somehow save the parsed data into a variable, and then go from that use a different script to extract the data from that variable?

Or maybe can I do something like:

Find div with this ID -> find bold text within it

Or maybe:

Find div with ID -> take out the text "Welcome to calculator"


Solution

  • echo $html->find('.firststyle b', 0)->innertext;
    #=> What is one plus two?