phpweb-scrapingecho

php scrape: echo the third word only


So I'm experimenting with php scraping possibilites, and I'd like to get a bit more precise about the data that is being echo'd. The below code is achieving this output: DJIA All Time, Record-High Close: June 14, 2017 (21,374.56)

<?php

// weather forecast los angeles
$doc = new DOMDocument;

// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;


$doc->strictErrorChecking = false;
$doc->recover = true;

$doc->loadHTMLFile('https://finance.yahoo.com/quote/%5EGSPC?p=%5EGSPC');

$xpath = new DOMXPath($doc);

$query = "//span[@class='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)']";

$entries = $xpath->query($query);
foreach ($entries as $entry) {
echo trim($entry->textContent);  // use `trim` to eliminate spaces
}
?>

Is there a possible way anyone knows where I could specify to only output the third word/value that comes up during the scrape. So in the above example, only "Time" would end up getting echo'd. When I'm saying word/value, I guess I'm identifying that as having a " " blank space.

Could this be possible? Sorry about my lack of php skills, quite new to this. If I was able to get this precise hwoever in what is being echo'd, that would raise many possibilities.

Best, -Wilson


Solution

  • You can use explode() php function, which uses to convert a string into an array.

    $entries = $xpath->query($query);
    foreach ($entries as $entry) {
         $result = trim($entry->textContent); 
         $ret_ = explode(' ', $result);
        echo $ret_[2];
    
    }
    
    Another one
    
    <?php
    
    $text = "DJIA All Time, Record-High Close: June 14, 2017 (21,374.56)";
    
    $text_array = explode(' ', $text);
    
     print_r($text_array);
     echo $text_array[2];
    ?>