Using Simple Html Dom to extract a specific bold text from a div

Discussion in 'PHP' started by zach catskilson, Feb 19, 2017.

  1. #1
    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>
    HTML:
    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]');
    PHP:
    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 -> get everything but the text "Welcome to calculator"
     
    zach catskilson, Feb 19, 2017 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #2
    you can step through the children of a div
     
    sarahk, Feb 19, 2017 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    This seems to work:
    
    <?php
    
    include 'simple_html_dom.php';
    
    $response = '<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>';
    
    $html = str_get_html($response);
    echo $the_question = $html->find('.class-coll-1',0)->children(1);
    
    ?>
    
    PHP:
    Never really used Simple HTML DOM before, this took me about 5 minutes, including downloading it, reading the manual, and making a test-file...
     
    PoPSiCLe, Feb 19, 2017 IP
  4. zach catskilson

    zach catskilson Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Thanks for the reply.
    Tried this method, the scripted works when you put the html as the $response variable but for some reason didn't work when I tried to grab the html from the webpage.

    However, what did work is the following script:

    $html->find('.firststyle b',0)
     
    zach catskilson, Feb 20, 2017 IP