【问题标题】:ob_start callback function extract output - PHPob_start 回调函数提取输出 - PHP
【发布时间】:2011-03-17 05:49:27
【问题描述】:

我想获得输出“Apples”,因为它位于 span 标签内,其 id 名为fruit。那么那个回调函数应该写什么代码呢?

 <?php
    function callback($buffer) {  
      // get the fruit inHTML text, the output should be "Apples" only   
        ....
        ....
    }
    ob_start("callback");
    ?> 
   <html>
<body>
 <p>It's like comparing <span id="fruit">Apples</span> to Oranges.</p> 
</body> 
</html> 
<?php
    ob_end_flush();
    ?>

【问题讨论】:

    标签: php function callback ob-start


    【解决方案1】:
    $dom = new DOMDocument;
    
    $dom->loadHTML($buffer);
    
    $xpath = new DOMXPath($dom);
    
    $node = $xpath->query('//span[@id="fruit"]');
    
    var_dump($node->item(0)->nodeValue); // string(6) "Apples"
    

    更通用的解决方案...

    $dom = new DOMDocument;
    
    $dom->loadHTML($buffer);
    
    $text = $dom->getElementsByTagName('p')->item(0)->getElementsByTagName('span')->item(0)->nodeValue;
    
    var_dump($text); // string(6) "Apples"
    

    【讨论】:

    • 如何删除这个“string(6)”,我只想要“Apples”。
    猜你喜欢
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多