Search code examples
phpwebnumbersscreen-scrapingforward

variable echoing out the last of 3 numbers


Hi so I've currently got a output echoing 176 8 58 from a web scraping script. I want to pack this script up into a variable and echo it out in other places on the website.

I've packed this up by doing this

ob_start();
echo $node->nodeValue. "\n";
$thenumbers = ob_get_contents();
ob_end_clean();

but when I echo it out like this

Now on the website the numbers are in spans and are split up by "/" do I need to do anything fancy? I'm kind of new to PHP so let me know if its something stupid!

<?php echo $thenumbers ?>

my output is then 176 8 58

Would really appreciate a bit of help

(web scraping script i'm using had to hide the website i'm scraping as its in development)

<?php
$teamlink = rwmb_meta( 'WEBSITE_HIDDEN' );

$arr = array( $teamlink ); 

foreach ($arr as &$value) {
    $file = $DOCUMENT_ROOT. $value;
    $doc = new DOMDocument();
    $doc->loadHTMLFile($file);
    $xpath = new DOMXpath($doc);

    $elements = $xpath->query("//*[contains(@class, 'table')]/tr[3]/td[3]/span");

    if (!is_null($elements)) {
        foreach ($elements as $element) {
            $nodes = $element->childNodes;
            foreach ($nodes as $node) {
                ob_start();
                echo $node->nodeValue. "\n";
                $win_loss = ob_get_contents();
                ob_end_clean();
            }
        }
    }
}
?>

p.s I know the script works as its currently outputting standard text fine.


Solution

  • My apoligies if I have completely misunderstood your question.

    If you want to add a "/" between the numbers, where the spaces are you could:

     echo str_replace(' ','/',$thenumbers);
    

    If you just want to show the last 3 digits (cleaning out the spaces from the string) you could;

     echo substr(str_replace(' ','',$thenumbers),-3);