2017-09-14 26 views
1

我需要一些幫助,使用我的php。我試圖從html標籤中獲取名爲streams的元素列表。當我嘗試從html標籤獲取元素時,它不會獲取它,因爲它會顯示爲空白。試圖從html標記中獲取元素時的空白元素

下面是代碼:

<?php 
    //ini_set('max_execution_time', 1000); 
    error_reporting(0); 
    //$errmsg_arr = array(); 
    //$errflag = false; 
    $baseUrl = file_get_contents('http://www.example.com/streams.php'); 
    $domdoc = new DOMDocument(); 
    $domdoc->strictErrorChecking = false; 
    $domdoc->recover=true; 
    @$domdoc->loadHTML($baseUrl); 
    $links = $domdoc->getElementById('streams'); 
    echo $links; 
?> 

下面是HTML源:

<p id='channels'>BBC One South E</p><p id='streams'>http://www.example.com/live/35vlz78As8/191</p> 

能否請你幫我我怎麼能夠從HTML的id獲取元素標籤?

+1

..?請添加到您的代碼 – scaisEdge

+0

我很抱歉,但它顯示爲空白。 –

+0

顯示您用於打印/回顯結果的代碼 – scaisEdge

回答

1

在開始的$刪除@ domdc,所以你可以看到,如果某些錯誤發生,

你如何顯示的結果,您應該使用的textContent

$domdoc->loadHTML($baseUrl); 
$links = $domdoc->getElementById('streams'); 

echo $links->textContent; 
+0

非常感謝你,因爲它工作得很好。我的html源代碼中有多個元素,所以如何爲每個元素輸出? –

+0

然後標籤的ID必須唯一..所以你應該使用類而不是ID ..並使用正確的方式,你可以迭代收集..例如:foreach ..如果我的回答是正確的,請將其標記爲接受...看看這裏如何 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – scaisEdge