简体   繁体   中英

PHP - DOM Parser will not return string

OK, I have an HTML DOM Parser. It works... sorta. It's trying to get the text from a div witch has a class.

See the main file

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html" />

  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
  </script>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js">
  </script>
  <style>
    #txt_out {
        border: 2px solid #C2C2C2;
        color: #2E2E2E;
        background: #EDEDED;
        width: 97%;
        padding: 5px;
        font-size: 12px;
        font-family: monospace;
        outline: none;
        height: 800px;
        margin: 10px 0;
      }
  </style>
  <title>Get Ranks</title>

  <script type="text/javascript" >   
    $(document).on("click", ".go", function (event) {

        var ID = $(".rank").val();
        console.log("Loading rank: " + ID);
        loadData(ID);


    });


    function loadData(ID) {
        var getRank = ID;

        var dataString = 'getRank=' + getRank;

        $.ajax({
            type: "POST",
            url: "otherTest.php",
            data: dataString,
            cache: false,
            success: function (html) {

                $("#txt_out").append(html);

            }
        });
    }
    </script>
  </head>
  <body>
    <h1>Get Mens Rankings</h1>

    <input value="233" class="rank" />

    <button class="go">Get stats</button>

    <textarea readonly="readonly" id="txt_out"></textarea>

  </body>
</html>

and upon clicking the 'get stats' button, it calls (through AJAX) this page:

<?php
include_once ('simple_html_dom.php');

$rank = $_POST['getRank'];

$URL = "http://fifa.com/worldranking/rankingtable/gender=m/rank=".$rank."/confederation=25998/page=1/_ranking_table.html";

$html = file_get_html($URL);

$test = trim($html->find('.rnkdate', 0)->innertext);
echo "Date published: " . $test;

?>

It's trying to extract the date published from this URL: http://www.fifa.com/worldranking/rankingtable/gender=m/rank=233/confederation=25998/page=1/_ranking_table.html where rank=XXX is a different table for a different month.

Anyway, when I do this, here's what I get:

在此处输入图片说明

The problem is that it's inside another DIV and thus... I guess it doesn't show inside a <textarea> . So... how do I get inside that div and extract the text itself? Thank you.

如果您使用的是http://simplehtmldom.sourceforge.net/ ,那么根据http://simplehtmldom.sourceforge.net/manual_api.htm看来,您应该使用“纯文本”,而不是“内部文本”(作为内部文本)似乎是javascript开发人员称为innerHTML的内容,而“ plaintext”似乎是.textContent的js等效语言……这个simplehtmldom lib似乎是由非Web开发人员编写的。

$test = trim($html->find('.rnkdate', 0)->plaintext);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM