douchuza8908 2014-11-07 16:29
浏览 41
已采纳

变量回显3个数字中的最后一个

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. "
";
$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. "
";
                $win_loss = ob_get_contents();
                ob_end_clean();
            }
        }
    }
}
?>

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

  • 写回答

1条回答 默认 最新

  • dongwen7813 2014-11-07 16:35
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?