2016-03-02 63 views
2

我正在使用簡單的HTML DOM解析器庫來解析帶有DOCTYPE標記的URL。我能夠達到所需的標籤,但每當我回應或嘗試存儲結果時,它都不會顯示任何內容。但是,var_dump()顯示我需要的字符串。Var_dump()顯示String,echo()/ print_r()返回Nothing

require_once("simpledom.php"); 

$url="http://google.com"; 
$sPage = file_get_contents($url); 
$sPageContent = new simple_html_dom(); 
$sPageContent->load($sPage); 
$sObjects = $sPageContent->find('unknown'); 

foreach($sObjects as $sKey) 
{ 
    var_dump($sKey->_[4]); /*this var_dump shows the stuff */ 
    $showres = $sKey->_[4] 
} 

/* this variable should hold the string but it shows nothing */ 
echo $showres; 
+1

什麼類型的var_dump顯示你? – PeterPan666

+0

var_dump顯示了字符串'<!doctype html>'(length = 15) –

+0

您是否試圖在循環內回顯,就像您在做var_dump一樣? –

回答

3

當輸出<!doctype html>時,瀏覽器讀取該元素,因此您需要對<>符號進行編碼。 PHP有一個已經建立的功能,http://php.net/manual/en/function.htmlspecialchars.php

所以,你的代碼應該是:

echo htmlspecialchars($showres); 

這在源會給你

&lt;!doctype html&gt; 
-2

嘗試值鑄造成一個字符串

$showres = (string)$sKey->_[4]; 

此外,你的迴音是你的循環後

+1

這是一個已經OP說過的字符串; 'var_dump顯示我字符串'<!doctype html>'(length = 15)'。 – chris85

+1

該值已經是一個字符串,請參閱第二條評論。 – PeterPan666

+0

對不起@David,它沒有幫助 –

1
echo htmlentities($showres); 

迴音必什麼在$showres和替換HTML實體,因此所有的HTML標記您可以看到該字符串,並且不會讓瀏覽器將其用作標記。

不知道你想要做什麼。